Typescript array typing

Hey I was wondering why typescript doesn't indicate that the name here is possibly undefined when the arr is empty?
type T = { name: string }[];

const arr: T = [];

const name = arr[0].name; // no ts error but it throws Cannot read properties of undefined
type T = { name: string }[];

const arr: T = [];

const name = arr[0].name; // no ts error but it throws Cannot read properties of undefined
5 Replies
cje
cje2y ago
depends on your tsconfig
cje
cje2y ago
TSConfig Reference - Docs on every TSConfig option
From allowJs to useDefineForClassFields the TSConfig reference includes information about all of the active compiler flags setting up a TypeScript project.
stanisław
stanisławOP2y ago
Thats what I thought Strange that default create next app doesnt have this option turned on Thanks
cje
cje2y ago
create next app has a history of not being great at ts by default
JacobMGEvans
JacobMGEvans2y ago
They are improving. Just by having TS as default, I'll take that W 😆
Want results from more Discord servers?
Add your server