Reddit Reddit reviews Effective TypeScript: 62 Specific Ways to Improve Your TypeScript

We found 1 Reddit comments about Effective TypeScript: 62 Specific Ways to Improve Your TypeScript. Here are the top ones, ranked by their Reddit score.

Computers & Technology
Books
Computer Programming
Software Design, Testing & Engineering
Software Development
Effective TypeScript: 62 Specific Ways to Improve Your TypeScript
Check price on Amazon

1 Reddit comment about Effective TypeScript: 62 Specific Ways to Improve Your TypeScript:

u/tehnologie ยท 1 pointr/typescript

Actually, there is no problem in sending an object:

const obj1 = {
name: "obj1",
otherField: "whatever"
};

...as a parameter to a function that takes an object of type:

type MyType = {
name: string
};

typeof obj1 is a subtype of MyType, as it includes all the properties of MyType (namely name: string).

I just tested that code with strict set to true, and the only error I get is inside foo, when you try to:

console.log(bar.otherField)

At that point, TypeScript has no way of knowing that bar has .otherField at runtime, and it's a perfectly legitimate complaint on its part. If you want foo to execute logic specific to typeof obj1 and to do something with the result, it's a good use case for higher order functions.

e.g.

const foo2 = <T extends MyType>(obj: T, fn: (obj: T) => / whatever fn should return /) => / implementation /

I highly recommend the book Effective TypeScript: 62 Specific Ways to Improve Your TypeScript