Generic values
TypeScript supports generic type/interface/class declarations. These features serve as an analog of "type constructors" in other type systems: they allow us to declare a type that is parameterized over some number of type arguments. TypeScript also supports generic functions. These serve as limited form of parametric polymorphism: they allow us to declare types whose *inhabitants* are parametrized over some number of type arguments. Unlike in certain other type systems however, these types can only be function types. So for example while we can declare: ```ts type Id = <T>(x : T) => T const test : Id = x => x ``` we cannot declare (for whatever reason): ```ts type TwoIds = <T> { id1: (x: T) => T, id2: (x: T) => T } const test : TwoIds = { id1: x => x, id2: x => x } ``` One result of this limitation is that the type constructor and polymorphism features interact poorly in TypeScript. The problem applies even to function types: if we abstract a sophisticated function type into a type constructor, we can no longer universally quantify away its type parameters to get a generic function type. This is illustrated below: ```ts // This works type Id = <T>(x : T) => T // This should also work type IdT<T> = (x: T) => T type Id = <T> IdT<T> // but is impossible to express ``` Another problem is that it is often useful to quantify over types other than bare functions, and TypeScript prohibits this. As an example, this prevents us from modeling polymorphic lenses: ```ts type L
Posted by masaeedu on Aug 3, 2017, 12:10 AM
Tags
Related issues
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
Generic values
TypeScript supports generic type/interface/class declarations. These features serve as an analog of "type constructors" in other type systems: they allow us to declare a type that is parameterized over some number of type arguments. TypeScript also supports generic functions. These serve as limited form of parametric polymorphism: they allow us to declare types whose *inhabitants* are parametrized over some number of type arguments. Unlike in certain other type systems however, these types can only be function types. So for example while we can declare: ```ts type Id = <T>(x : T) => T const test : Id = x => x ``` we cannot declare (for whatever reason): ```ts type TwoIds = <T> { id1: (x: T) => T, id2: (x: T) => T } const test : TwoIds = { id1: x => x, id2: x => x } ``` One result of this limitation is that the type constructor and polymorphism features interact poorly in TypeScript. The problem applies even to function types: if we abstract a sophisticated function type into a type constructor, we can no longer universally quantify away its type parameters to get a generic function type. This is illustrated below: ```ts // This works type Id = <T>(x : T) => T // This should also work type IdT<T> = (x: T) => T type Id = <T> IdT<T> // but is impossible to express ``` Another problem is that it is often useful to quantify over types other than bare functions, and TypeScript prohibits this. As an example, this prevents us from modeling polymorphic lenses: ```ts type L
Comments (0)
No comments yet. Start the discussion.