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.
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.
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 ofMyType
, as it includes all the properties ofMyType
(namelyname: string
).I just tested that code with
strict
set totrue
, and the only error I get is insidefoo
, 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 wantfoo
to execute logic specific totypeof 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