Live data from Hacker News

TypeScript 3.7

typescriptlang.org

21–30 of 167 posts

Re: TypeScript 3.7

#22
post #15

Earlier 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`

https://www.npmjs.com/package/typesafe-get works pretty good

Re: TypeScript 3.7

#23
For 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"?

Re: TypeScript 3.7

#24
post #13

I 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.

I'm assuming by optional operator you're referring to optional chaining? If so, it's very cool but strikes me as an odd reason to move to TypeScript, because it's a stage 3 proposal in JavaScript too, so is likely to be widely supported soon.

Re: TypeScript 3.7

#25
post #13

I 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.

I can almost guarantee you'll like it. Kotlin has this same feature and I've found it saves a good amount of boilerplate null checking when inter-operating with lousy legacy Java code.

Re: TypeScript 3.7

#26

For 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"?

Not really https://github.com/microsoft/TypeScript/issues/9235

Though with tuples, etc. being defined, maybe it's worth re-examining.

Re: TypeScript 3.7

#27

For 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"?

For array types, I don't think there's a way. You can type x as a tuple like this:

    const x: [number, number] = [1, 2];
In which case you'll only be able to access indices zero and one.

Re: TypeScript 3.7

#28

For 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"?

https://www.typescriptlang.org/play/index.html#code/MYewdgzg...

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

#29

For 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"?

Perhaps explicitly setting the types might do it?

    const x: number[]= [1,2]
    const y: number | undefined = x[666]
    const z: number = y + 3
EDIT: Just tried, still works

Re: TypeScript 3.7

#30
post #15

Earlier 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`

In lodash's defense, there is a @types package for it, which has a typed _.get. However, in my experience, the lodash typings are really difficult to use correctly.

Definitely will be replacing usage of _.get with optional chaining soon.

Post reply on HN