mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-10 06:41:59 -06:00
Merge pull request #1443 from Microsoft/improvedTypeInference
Make initial inferences from parameterless function expressions
This commit is contained in:
commit
b2a02fe9ef
@ -3355,7 +3355,7 @@ module ts {
|
||||
}
|
||||
|
||||
function isContextSensitiveFunctionLikeDeclaration(node: FunctionLikeDeclaration) {
|
||||
return !node.typeParameters && !forEach(node.parameters, p => p.type);
|
||||
return !node.typeParameters && node.parameters.length && !forEach(node.parameters, p => p.type);
|
||||
}
|
||||
|
||||
function getTypeWithoutConstructors(type: Type): Type {
|
||||
|
||||
@ -1,14 +1,9 @@
|
||||
tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts(2,22): error TS2339: Property 'foo' does not exist on type 'string'.
|
||||
tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts(3,10): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
|
||||
Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts (2 errors) ====
|
||||
==== tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts (1 errors) ====
|
||||
var f10: <T>(x: T, b: () => (a: T) => void, y: T) => T;
|
||||
f10('', () => a => a.foo, ''); // a is string
|
||||
~~~
|
||||
!!! error TS2339: Property 'foo' does not exist on type 'string'.
|
||||
var r9 = f10('', () => (a => a.foo), 1); // error
|
||||
~~~
|
||||
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
|
||||
!!! error TS2453: Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'.
|
||||
var r9 = f10('', () => (a => a.foo), 1); // error
|
||||
@ -0,0 +1,17 @@
|
||||
//// [inferenceFromParameterlessLambda.ts]
|
||||
function foo<T>(o: Take<T>, i: Make<T>) { }
|
||||
interface Make<T> {
|
||||
(): T;
|
||||
}
|
||||
interface Take<T> {
|
||||
(n: T): void;
|
||||
}
|
||||
// Infer string from second argument because it isn't context sensitive
|
||||
foo(n => n.length, () => 'hi');
|
||||
|
||||
|
||||
//// [inferenceFromParameterlessLambda.js]
|
||||
function foo(o, i) {
|
||||
}
|
||||
// Infer string from second argument because it isn't context sensitive
|
||||
foo(function (n) { return n.length; }, function () { return 'hi'; });
|
||||
@ -0,0 +1,37 @@
|
||||
=== tests/cases/compiler/inferenceFromParameterlessLambda.ts ===
|
||||
function foo<T>(o: Take<T>, i: Make<T>) { }
|
||||
>foo : <T>(o: Take<T>, i: Make<T>) => void
|
||||
>T : T
|
||||
>o : Take<T>
|
||||
>Take : Take<T>
|
||||
>T : T
|
||||
>i : Make<T>
|
||||
>Make : Make<T>
|
||||
>T : T
|
||||
|
||||
interface Make<T> {
|
||||
>Make : Make<T>
|
||||
>T : T
|
||||
|
||||
(): T;
|
||||
>T : T
|
||||
}
|
||||
interface Take<T> {
|
||||
>Take : Take<T>
|
||||
>T : T
|
||||
|
||||
(n: T): void;
|
||||
>n : T
|
||||
>T : T
|
||||
}
|
||||
// Infer string from second argument because it isn't context sensitive
|
||||
foo(n => n.length, () => 'hi');
|
||||
>foo(n => n.length, () => 'hi') : void
|
||||
>foo : <T>(o: Take<T>, i: Make<T>) => void
|
||||
>n => n.length : (n: string) => number
|
||||
>n : string
|
||||
>n.length : number
|
||||
>n : string
|
||||
>length : number
|
||||
>() => 'hi' : () => string
|
||||
|
||||
9
tests/cases/compiler/inferenceFromParameterlessLambda.ts
Normal file
9
tests/cases/compiler/inferenceFromParameterlessLambda.ts
Normal file
@ -0,0 +1,9 @@
|
||||
function foo<T>(o: Take<T>, i: Make<T>) { }
|
||||
interface Make<T> {
|
||||
(): T;
|
||||
}
|
||||
interface Take<T> {
|
||||
(n: T): void;
|
||||
}
|
||||
// Infer string from second argument because it isn't context sensitive
|
||||
foo(n => n.length, () => 'hi');
|
||||
Loading…
x
Reference in New Issue
Block a user