s3 type error

Not great at typescript so I'm not sure why this is throwing a type error.
export const r2 =
globalForR2.r2 ||
new S3Client({
region: "auto",
endpoint: process.env.AWS_ENDPOINT,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
},
});
export const r2 =
globalForR2.r2 ||
new S3Client({
region: "auto",
endpoint: process.env.AWS_ENDPOINT,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
},
});
No description
3 Replies
'tonyyprints'
'tonyyprints'9mo ago
apparently type assertions fixes it I'll just go with that i guess lol
Alejo
Alejo9mo ago
Env variables can be undefined, so Typescript's type for an env var is string | undefined, if you have a type string you cannot assign it string | undefined because string is not undefined, if you want to tell TS that a variable/property is NOT undefined, you can append ! at the end, so in this case you could do this and should be fine:
export const r2 =
globalForR2.r2 ||
new S3Client({
region: "auto",
endpoint: process.env.AWS_ENDPOINT!,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
},
});
export const r2 =
globalForR2.r2 ||
new S3Client({
region: "auto",
endpoint: process.env.AWS_ENDPOINT!,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
},
});
'tonyyprints'
'tonyyprints'9mo ago
awesome, makes sense. Thank you