Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35092,8 +35092,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// We clone the inferenceContext to avoid fixing. For example, when the source signature is <T>(x: T) => T[] and
// the contextual signature is (...args: A) => B, we want to infer the element type of A's constraint (say 'any')
// for T but leave it possible to later infer '[any]' back to A.
const restType = getEffectiveRestType(contextualSignature);
const mapper = inferenceContext && (restType && restType.flags & TypeFlags.TypeParameter ? inferenceContext.nonFixingMapper : inferenceContext.mapper);
const mapper = inferenceContext?.nonFixingMapper;
const sourceSignature = mapper ? instantiateSignature(contextualSignature, mapper) : contextualSignature;
applyToParameterTypes(sourceSignature, signature, (source, target) => {
// Type parameters from outer context referenced by source type are fixed by instantiation of the source type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ contextualSignatureInstantiation.ts(19,13): error TS2345: Argument of type '<T>(
Types of parameters 'y' and 'y' are incompatible.
Type 'string' is not assignable to type 'number'.
contextualSignatureInstantiation.ts(20,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'unknown'.
contextualSignatureInstantiation.ts(20,23): error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: number, y: string) => number'.
contextualSignatureInstantiation.ts(20,23): error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: number, y: "one") => number'.
Types of parameters 'y' and 'y' are incompatible.
Type 'string' is not assignable to type 'number'.
contextualSignatureInstantiation.ts(21,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'unknown'.
contextualSignatureInstantiation.ts(21,23): error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: string, y: number) => string'.
contextualSignatureInstantiation.ts(21,23): error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: string, y: 1) => string'.
Types of parameters 'y' and 'y' are incompatible.
Type 'number' is not assignable to type 'string'.
contextualSignatureInstantiation.ts(27,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'number'.
contextualSignatureInstantiation.ts(28,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'number'.


==== contextualSignatureInstantiation.ts (6 errors) ====
==== contextualSignatureInstantiation.ts (8 errors) ====
// TypeScript Spec, section 4.12.2:
// If e is an expression of a function type that contains exactly one generic call signature and no other members,
// and T is a function type with exactly one non - generic call signature and no other members, then any inferences
Expand Down Expand Up @@ -44,19 +46,31 @@ contextualSignatureInstantiation.ts(21,23): error TS2345: Argument of type '<T>(
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'unknown'.
!!! related TS6203 contextualSignatureInstantiation.ts:18:5: 'b' was also declared here.
~
!!! error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: number, y: string) => number'.
!!! error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: number, y: "one") => number'.
!!! error TS2345: Types of parameters 'y' and 'y' are incompatible.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
var b = bar("one", 1, g); // Error, number and string are disjoint types
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'unknown'.
!!! related TS6203 contextualSignatureInstantiation.ts:18:5: 'b' was also declared here.
~
!!! error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: string, y: number) => string'.
!!! error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: string, y: 1) => string'.
!!! error TS2345: Types of parameters 'y' and 'y' are incompatible.
!!! error TS2345: Type 'number' is not assignable to type 'string'.
var b = baz(b, b, g); // Should be number | string

var one: 1 = 1
var num: number = 10;

var b = bar(one, num, g); // ok
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'number'.
!!! related TS6203 contextualSignatureInstantiation.ts:18:5: 'b' was also declared here.
var b = bar(num, one, g); // ok
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'number'.
!!! related TS6203 contextualSignatureInstantiation.ts:18:5: 'b' was also declared here.

var d: number[] | string[];
var d = foo(h); // Should be number[] | string[]
var d = bar(1, "one", h); // Should be number[] | string[]
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/contextualSignatureInstantiation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ var b = bar(1, "one", g); // Error, number and string are disjoint types
var b = bar("one", 1, g); // Error, number and string are disjoint types
var b = baz(b, b, g); // Should be number | string

var one: 1 = 1
var num: number = 10;

var b = bar(one, num, g); // ok
var b = bar(num, one, g); // ok

var d: number[] | string[];
var d = foo(h); // Should be number[] | string[]
var d = bar(1, "one", h); // Should be number[] | string[]
Expand All @@ -45,6 +51,10 @@ var b = foo(g); // Error, number and string are disjoint types
var b = bar(1, "one", g); // Error, number and string are disjoint types
var b = bar("one", 1, g); // Error, number and string are disjoint types
var b = baz(b, b, g); // Should be number | string
var one = 1;
var num = 10;
var b = bar(one, num, g); // ok
var b = bar(num, one, g); // ok
var d;
var d = foo(h); // Should be number[] | string[]
var d = bar(1, "one", h); // Should be number[] | string[]
Expand Down
48 changes: 34 additions & 14 deletions tests/baselines/reference/contextualSignatureInstantiation.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -83,52 +83,72 @@ var a = baz(1, 1, g); // Should be number
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var b: number | string;
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3) ... and 2 more)

var b = foo(g); // Error, number and string are disjoint types
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3) ... and 2 more)
>foo : Symbol(foo, Decl(contextualSignatureInstantiation.ts, 0, 0))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var b = bar(1, "one", g); // Error, number and string are disjoint types
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3) ... and 2 more)
>bar : Symbol(bar, Decl(contextualSignatureInstantiation.ts, 6, 60))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var b = bar("one", 1, g); // Error, number and string are disjoint types
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3) ... and 2 more)
>bar : Symbol(bar, Decl(contextualSignatureInstantiation.ts, 6, 60))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var b = baz(b, b, g); // Should be number | string
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3) ... and 2 more)
>baz : Symbol(baz, Decl(contextualSignatureInstantiation.ts, 7, 68))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3) ... and 2 more)
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3) ... and 2 more)
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var one: 1 = 1
>one : Symbol(one, Decl(contextualSignatureInstantiation.ts, 23, 3))

var num: number = 10;
>num : Symbol(num, Decl(contextualSignatureInstantiation.ts, 24, 3))

var b = bar(one, num, g); // ok
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3) ... and 2 more)
>bar : Symbol(bar, Decl(contextualSignatureInstantiation.ts, 6, 60))
>one : Symbol(one, Decl(contextualSignatureInstantiation.ts, 23, 3))
>num : Symbol(num, Decl(contextualSignatureInstantiation.ts, 24, 3))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var b = bar(num, one, g); // ok
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3) ... and 2 more)
>bar : Symbol(bar, Decl(contextualSignatureInstantiation.ts, 6, 60))
>num : Symbol(num, Decl(contextualSignatureInstantiation.ts, 24, 3))
>one : Symbol(one, Decl(contextualSignatureInstantiation.ts, 23, 3))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var d: number[] | string[];
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 29, 3), Decl(contextualSignatureInstantiation.ts, 30, 3), Decl(contextualSignatureInstantiation.ts, 31, 3), Decl(contextualSignatureInstantiation.ts, 32, 3), Decl(contextualSignatureInstantiation.ts, 33, 3))

var d = foo(h); // Should be number[] | string[]
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 29, 3), Decl(contextualSignatureInstantiation.ts, 30, 3), Decl(contextualSignatureInstantiation.ts, 31, 3), Decl(contextualSignatureInstantiation.ts, 32, 3), Decl(contextualSignatureInstantiation.ts, 33, 3))
>foo : Symbol(foo, Decl(contextualSignatureInstantiation.ts, 0, 0))
>h : Symbol(h, Decl(contextualSignatureInstantiation.ts, 10, 37))

var d = bar(1, "one", h); // Should be number[] | string[]
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 29, 3), Decl(contextualSignatureInstantiation.ts, 30, 3), Decl(contextualSignatureInstantiation.ts, 31, 3), Decl(contextualSignatureInstantiation.ts, 32, 3), Decl(contextualSignatureInstantiation.ts, 33, 3))
>bar : Symbol(bar, Decl(contextualSignatureInstantiation.ts, 6, 60))
>h : Symbol(h, Decl(contextualSignatureInstantiation.ts, 10, 37))

var d = bar("one", 1, h); // Should be number[] | string[]
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 29, 3), Decl(contextualSignatureInstantiation.ts, 30, 3), Decl(contextualSignatureInstantiation.ts, 31, 3), Decl(contextualSignatureInstantiation.ts, 32, 3), Decl(contextualSignatureInstantiation.ts, 33, 3))
>bar : Symbol(bar, Decl(contextualSignatureInstantiation.ts, 6, 60))
>h : Symbol(h, Decl(contextualSignatureInstantiation.ts, 10, 37))

var d = baz(d, d, g); // Should be number[] | string[]
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 29, 3), Decl(contextualSignatureInstantiation.ts, 30, 3), Decl(contextualSignatureInstantiation.ts, 31, 3), Decl(contextualSignatureInstantiation.ts, 32, 3), Decl(contextualSignatureInstantiation.ts, 33, 3))
>baz : Symbol(baz, Decl(contextualSignatureInstantiation.ts, 7, 68))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 29, 3), Decl(contextualSignatureInstantiation.ts, 30, 3), Decl(contextualSignatureInstantiation.ts, 31, 3), Decl(contextualSignatureInstantiation.ts, 32, 3), Decl(contextualSignatureInstantiation.ts, 33, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 29, 3), Decl(contextualSignatureInstantiation.ts, 30, 3), Decl(contextualSignatureInstantiation.ts, 31, 3), Decl(contextualSignatureInstantiation.ts, 32, 3), Decl(contextualSignatureInstantiation.ts, 33, 3))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

40 changes: 40 additions & 0 deletions tests/baselines/reference/contextualSignatureInstantiation.types
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,46 @@ var b = baz(b, b, g); // Should be number | string
>g : <T>(x: T, y: T) => T
> : ^ ^^ ^^ ^^ ^^ ^^^^^

var one: 1 = 1
>one : 1
> : ^
>1 : 1
> : ^

var num: number = 10;
>num : number
> : ^^^^^^
>10 : 10
> : ^^

var b = bar(one, num, g); // ok
>b : string | number
> : ^^^^^^^^^^^^^^^
>bar(one, num, g) : number
> : ^^^^^^
>bar : <T, U, V>(x: T, y: U, cb: (x: T, y: U) => V) => V
> : ^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^^^^
>one : 1
> : ^
>num : number
> : ^^^^^^
>g : <T>(x: T, y: T) => T
> : ^ ^^ ^^ ^^ ^^ ^^^^^

var b = bar(num, one, g); // ok
>b : string | number
> : ^^^^^^^^^^^^^^^
>bar(num, one, g) : number
> : ^^^^^^
>bar : <T, U, V>(x: T, y: U, cb: (x: T, y: U) => V) => V
> : ^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^^^^
>num : number
> : ^^^^^^
>one : 1
> : ^
>g : <T>(x: T, y: T) => T
> : ^ ^^ ^^ ^^ ^^ ^^^^^

var d: number[] | string[];
>d : number[] | string[]
> : ^^^^^^^^^^^^^^^^^^^
Expand Down
Loading