IssueRaiser

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

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

github (Other)

Generic values

masaeeduAug 3, 2017, 12:10 AM

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.

Comment on this issue