5 ms·
Offtopic but what's nice about Typescript generics? Typescript doesn't even let you specify variance. It's one of the unsound parts of the language's type syste
by nikitaga 6y ago
Offtopic but what's nice about Typescript generics? Typescript doesn't even let you specify variance. It's one of the unsound parts of the language's type system in fact.
- IggleSniggle 6y agoYou cannot explicitly specify covariant vs contravariant, but TypeScript absolutely does allow you to express these relationships. Unless I misunderstand you. That said, the type system has come a very long way even in just the last year. The biggest improvements imho being around recursive types (which was one of the biggest holes for a long time imo), literal types / literal type inference, and tuple types. It's not complete by any means, but it's improving quite rapidly.
- nikitaga 6y agoIt appears that function parameter bivariance is still a thing? [1] Although there seems to now be a flag to make this one use of variance correct. I would assume even Array<T> is still bivariant as well... Both of those are horribly unsound, just for convenience. Sure convenience and compatibility are Typescript's ultimate goals, but to actually praise it for its generics? That's very strange. > TypeScript absolutely does allow you to express these relationships How would you express a class with covariant or contravariant type params in Typescript? [1] https://www.typescriptlang.org/docs/handbook/type-compatibility.html#function-parameter-bivariance https://www.typescriptlang.org/docs/handbook/type-compatibil...
- flatline 6y agoIt is not the most feature-rich generic system but it's come a long way since the earliest revisions and does have some niceties: https://www.typescriptlang.org/docs/handbook/advanced-types.html https://www.typescriptlang.org/docs/handbook/advanced-types....