TypeScript 3.7
21–30 of 167 posts
Re: TypeScript 3.7
#22Earlier quoted context omitted.
I just use lodash get when I access object properties
`get` will destroy type inference, there's no workaround. Your output types are always `any`. The new optional chaining fixes that problem, giving you the same terseness as `get`
Re: TypeScript 3.7
#23 const x = [1,2];
const y = x[666];
const z = y + 3;
Is there a way for TypeScript to flag the last line as a type error?TypeScript will say "y" has type "number" when "x[666]" returns undefined. Why does TypeScript not say the type of "y" is "number | undefined"?
Re: TypeScript 3.7
#24I really like the new optional operator, this might be what gets me to bite the bullet and start moving some of my projects over to typescript - dealing with potential undefined objects in those chains is one of the things I actively dislike about writing in vanilla Javascript.
Re: TypeScript 3.7
#25I really like the new optional operator, this might be what gets me to bite the bullet and start moving some of my projects over to typescript - dealing with potential undefined objects in those chains is one of the things I actively dislike about writing in vanilla Javascript.
Re: TypeScript 3.7
#26For this code: const x = [1,2]; const y = x[666]; const z = y + 3; Is there a way for TypeScript to flag the last line as a type error? TypeScript will say "y" has type "number" when "x[666]" returns undefined. Why does TypeScript not say the type of "y" is "number | undefined"?
Though with tuples, etc. being defined, maybe it's worth re-examining.
Re: TypeScript 3.7
#27For this code: const x = [1,2]; const y = x[666]; const z = y + 3; Is there a way for TypeScript to flag the last line as a type error? TypeScript will say "y" has type "number" when "x[666]" returns undefined. Why does TypeScript not say the type of "y" is "number | undefined"?
const x: [number, number] = [1, 2];
In which case you'll only be able to access indices zero and one.Re: TypeScript 3.7
#28For this code: const x = [1,2]; const y = x[666]; const z = y + 3; Is there a way for TypeScript to flag the last line as a type error? TypeScript will say "y" has type "number" when "x[666]" returns undefined. Why does TypeScript not say the type of "y" is "number | undefined"?
Using `as const` will report both the 2nd and 3rd lines as type errors. You've been able to do this in TypeScript for a while even before they introduced the `as const` syntax.
Re: TypeScript 3.7
#29For this code: const x = [1,2]; const y = x[666]; const z = y + 3; Is there a way for TypeScript to flag the last line as a type error? TypeScript will say "y" has type "number" when "x[666]" returns undefined. Why does TypeScript not say the type of "y" is "number | undefined"?
const x: number[]= [1,2]
const y: number | undefined = x[666]
const z: number = y + 3
EDIT: Just tried, still worksRe: TypeScript 3.7
#30Earlier quoted context omitted.
I just use lodash get when I access object properties
`get` will destroy type inference, there's no workaround. Your output types are always `any`. The new optional chaining fixes that problem, giving you the same terseness as `get`
Definitely will be replacing usage of _.get with optional chaining soon.