Skip to content

Type parameters as constraints #5949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 11, 2015
Merged
Prev Previous commit
Next Next commit
Adding test
  • Loading branch information
ahejlsberg committed Dec 10, 2015
commit 3055445d27c3d578e053541aae0fa30d7ab33191
15 changes: 15 additions & 0 deletions tests/baselines/reference/typeParameterConstraintInstantiation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [typeParameterConstraintInstantiation.ts]
// Check that type parameter constraints are properly instantiated

interface Mapper<T> {
map<U extends T, V extends U[]>(f: (item: T) => U): V;
}

var m: Mapper<string>;
var a = m.map((x: string) => x); // string[]


//// [typeParameterConstraintInstantiation.js]
// Check that type parameter constraints are properly instantiated
var m;
var a = m.map(function (x) { return x; }); // string[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/compiler/typeParameterConstraintInstantiation.ts ===
// Check that type parameter constraints are properly instantiated

interface Mapper<T> {
>Mapper : Symbol(Mapper, Decl(typeParameterConstraintInstantiation.ts, 0, 0))
>T : Symbol(T, Decl(typeParameterConstraintInstantiation.ts, 2, 17))

map<U extends T, V extends U[]>(f: (item: T) => U): V;
>map : Symbol(map, Decl(typeParameterConstraintInstantiation.ts, 2, 21))
>U : Symbol(U, Decl(typeParameterConstraintInstantiation.ts, 3, 8))
>T : Symbol(T, Decl(typeParameterConstraintInstantiation.ts, 2, 17))
>V : Symbol(V, Decl(typeParameterConstraintInstantiation.ts, 3, 20))
>U : Symbol(U, Decl(typeParameterConstraintInstantiation.ts, 3, 8))
>f : Symbol(f, Decl(typeParameterConstraintInstantiation.ts, 3, 36))
>item : Symbol(item, Decl(typeParameterConstraintInstantiation.ts, 3, 40))
>T : Symbol(T, Decl(typeParameterConstraintInstantiation.ts, 2, 17))
>U : Symbol(U, Decl(typeParameterConstraintInstantiation.ts, 3, 8))
>V : Symbol(V, Decl(typeParameterConstraintInstantiation.ts, 3, 20))
}

var m: Mapper<string>;
>m : Symbol(m, Decl(typeParameterConstraintInstantiation.ts, 6, 3))
>Mapper : Symbol(Mapper, Decl(typeParameterConstraintInstantiation.ts, 0, 0))

var a = m.map((x: string) => x); // string[]
>a : Symbol(a, Decl(typeParameterConstraintInstantiation.ts, 7, 3))
>m.map : Symbol(Mapper.map, Decl(typeParameterConstraintInstantiation.ts, 2, 21))
>m : Symbol(m, Decl(typeParameterConstraintInstantiation.ts, 6, 3))
>map : Symbol(Mapper.map, Decl(typeParameterConstraintInstantiation.ts, 2, 21))
>x : Symbol(x, Decl(typeParameterConstraintInstantiation.ts, 7, 15))
>x : Symbol(x, Decl(typeParameterConstraintInstantiation.ts, 7, 15))

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
=== tests/cases/compiler/typeParameterConstraintInstantiation.ts ===
// Check that type parameter constraints are properly instantiated

interface Mapper<T> {
>Mapper : Mapper<T>
>T : T

map<U extends T, V extends U[]>(f: (item: T) => U): V;
>map : <U extends T, V extends U[]>(f: (item: T) => U) => V
>U : U
>T : T
>V : V
>U : U
>f : (item: T) => U
>item : T
>T : T
>U : U
>V : V
}

var m: Mapper<string>;
>m : Mapper<string>
>Mapper : Mapper<T>

var a = m.map((x: string) => x); // string[]
>a : string[]
>m.map((x: string) => x) : string[]
>m.map : <U extends string, V extends U[]>(f: (item: string) => U) => V
>m : Mapper<string>
>map : <U extends string, V extends U[]>(f: (item: string) => U) => V
>(x: string) => x : (x: string) => string
>x : string
>x : string

8 changes: 8 additions & 0 deletions tests/cases/compiler/typeParameterConstraintInstantiation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Check that type parameter constraints are properly instantiated

interface Mapper<T> {
map<U extends T, V extends U[]>(f: (item: T) => U): V;
}

var m: Mapper<string>;
var a = m.map((x: string) => x); // string[]