Merge pull request #5949 from Microsoft/typeParametersAsConstraints

Type parameters as constraints
This commit is contained in:
Anders Hejlsberg
2015-12-10 16:53:59 -08:00
178 changed files with 12662 additions and 3206 deletions

View File

@@ -0,0 +1,8 @@
// @declaration: true
// Repro from #6040
function append<a, b extends a>(result: a[], value: b): a[] {
result.push(value);
return result;
}

View File

@@ -0,0 +1,22 @@
// Example from #6037
function fold<a, r>(values: a[], result: r, fold: (result: r, value: a) => r): r {
for (let value of values) {
result = fold(result, value);
}
return result;
}
function append<a, b extends a>(values: a[], value: b): a[] {
values.push(value);
return values;
}
fold(
[1, 2, 3],
[] as [string, string][],
(result, value) => append(
result,
["", ""]
)
);

View File

@@ -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[]