mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 04:43:37 -05:00
Merge pull request #5949 from Microsoft/typeParametersAsConstraints
Type parameters as constraints
This commit is contained in:
@@ -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;
|
||||
}
|
||||
22
tests/cases/compiler/typeInferenceFBoundedTypeParams.ts
Normal file
22
tests/cases/compiler/typeInferenceFBoundedTypeParams.ts
Normal 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,
|
||||
["", ""]
|
||||
)
|
||||
);
|
||||
@@ -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[]
|
||||
Reference in New Issue
Block a user