8000 Improve overload and generic signature inference: Inference alternatives and linked inferences by weswigham · Pull Request #52944 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content

Improve overload and generic signature inference: Inference alternatives and linked inferences #52944

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Some tests from some design limitation issues this fixes
  • Loading branch information
weswigham committed Feb 23, 2023
commit 4b8b0a5e36b68bc25971ec6249a0084678602007
25 changes: 25 additions & 0 deletions tests/baselines/reference/overloadedGenericInference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [overloadedGenericInference.ts]
// #35501
declare function fn(x: string): string;
declare function fn(x: string[]): string;

declare function map<A, R>(fn: (item: A) => R, list: A[]): R[];

const mapped = map(fn, ['1']);

// #30294 (partial fix)
declare class C {
static x(y: number): number;
static x(): number;
}
C.x.call(C, 1); // ok
C.x.call(C); // ok
C.x.call(1); // ok (not an error because the `this` type of `x` is not constrained)


//// [overloadedGenericInference.js]
"use strict";
const mapped = map(fn, ['1']);
C.x.call(C, 1); // ok
C.x.call(C); // ok
C.x.call(1); // ok (not an error because the `this` type of `x` is not constrained)
61 changes: 61 additions & 0 deletions tests/baselines/reference/overloadedGenericInference.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
=== tests/cases/compiler/overloadedGenericInference.ts ===
// #35501
declare function fn(x: string): string;
>fn : Symbol(fn, Decl(overloadedGenericInference.ts, 0, 0), Decl(overloadedGenericInference.ts, 1, 39))
>x : Symbol(x, Decl(overloadedGenericInference.ts, 1, 20))

declare function fn(x: string[]): string;
>fn : Symbol(fn, Decl(overloadedGenericInference.ts, 0, 0), Decl(overloadedGenericInference.ts, 1, 39))
>x : Symbol(x, Decl(overloadedGenericInference.ts, 2, 20))

declare function map<A, R>(fn: (item: A) => R, list: A[]): R[];
>map : Symbol(map, Decl(overloadedGenericInference.ts, 2, 41))
>A : Symbol(A, Decl(overloadedGenericInference.ts, 4, 21))
>R : Symbol(R, Decl(overloadedGenericInference.ts, 4, 23))
>fn : Symbol(fn, Decl(overloadedGenericInference.ts, 4, 27))
>item : Symbol(item, Decl(overloadedGenericInference.ts, 4, 32))
>A : Symbol(A, Decl(overloadedGenericInference.ts, 4, 21))
>R : Symbol(R, Decl(overloadedGenericInference.ts, 4, 23))
>list : Symbol(list, Decl(overloadedGenericInference.ts, 4, 46))
>A : Symbol(A, Decl(overloadedGenericInference.ts, 4, 21))
>R : Symbol(R, Decl(overloadedGenericInference.ts, 4, 23))

const mapped = map(fn, ['1']);
>mapped : Symbol(mapped, Decl(overloadedGenericInference.ts, 6, 5))
>map : Symbol(map, Decl(overloadedGenericInference.ts, 2, 41))
>fn : Symbol(fn, Decl(overloadedGenericInference.ts, 0, 0), Decl(overloadedGenericInference.ts, 1, 39))

// #30294 (partial fix)
declare class C {
>C : Symbol(C, Decl(overloadedGenericInference.ts, 6, 30))

static x(y: number): number;
>x : Symbol(C.x, Decl(overloadedGenericInference.ts, 9, 17), Decl(overloadedGenericInference.ts, 10, 32))
>y : Symbol(y, Decl(overloadedGenericInference.ts, 10, 13))

static x(): number;
>x : Symbol(C.x, Decl(overloadedGenericInference.ts, 9, 17), Decl(overloadedGenericInference.ts, 10, 32))
}
C.x.call(C, 1); // ok
>C.x.call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>C.x : Symbol(C.x, Decl(overloadedGenericInference.ts, 9, 17), Decl(overloadedGenericInference.ts, 10, 32))
>C : Symbol(C, Decl(overloadedGenericInference.ts, 6, 30))
>x : Symbol(C.x, Decl(overloadedGenericInference.ts, 9, 17), Decl(overloadedGenericInference.ts, 10, 32))
>call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>C : Symbol(C, Decl(overloadedGenericInference.ts, 6, 30))

C.x.call(C); // ok
>C.x.call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>C.x : Symbol(C.x, Decl(overloadedGenericInference.ts, 9, 17), Decl(overloadedGenericInference.ts, 10, 32))
>C : Symbol(C, Decl(overloadedGenericInference.ts, 6, 30))
>x : Symbol(C.x, Decl(overloadedGenericInference.ts, 9, 17), Decl(overloadedGenericInference.ts, 10, 32))
>call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>C : Symbol(C, Decl(overloadedGenericInference.ts, 6, 30))

C.x.call(1); // ok (not an error because the `this` type of `x` is not constrained)
>C.x.call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>C.x : Symbol(C.x, Decl(overloadedGenericInference.ts, 9, 17), Decl(overloadedGenericInference.ts, 10, 32))
>C : Symbol(C, Decl(overloadedGenericInference.ts, 6, 30))
>x : Symbol(C.x, Decl(overloadedGenericInference.ts, 9, 17), Decl(overloadedGenericInference.ts, 10, 32))
>call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))

63 changes: 63 additions & 0 deletions tests/baselines/reference/overloadedGenericInference.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
=== tests/cases/compiler/overloadedGenericInference.ts ===
// #35501
declare function fn(x: string): string;
>fn : { (x: string): string; (x: string[]): string; }
>x : string

declare function fn(x: string[]): string;
>fn : { (x: string): string; (x: string[]): string; }
>x : string[]

declare function map<A, R>(fn: (item: A) => R, list: A[]): R[];
>map : <A, R>(fn: (item: A) => R, list: A[]) => R[]
>fn : (item: A) => R
>item : A
>list : A[]

const mapped = map(fn, ['1']);
>mapped : string[]
>map(fn, ['1']) : string[]
>map : <A, R>(fn: (item: A) => R, list: A[]) => R[]
>fn : { (x: string): string; (x: string[]): string; }
>['1'] : string[]
>'1' : "1"

// #30294 (partial fix)
declare class C {
>C : C

static x(y: number): number;
>x : { (y: number): number; (): number; }
>y : number

static x(): number;
>x : { (y: number): number; (): number; }
}
C.x.call(C, 1); // ok
>C.x.call(C, 1) : number
>C.x.call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
>C.x : { (y: number): number; (): number; }
>C : typeof C
>x : { (y: number): number; (): number; }
>call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
>C : typeof C
>1 : 1

C.x.call(C); // ok
>C.x.call(C) : number
>C.x.call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
>C.x : { (y: number): number; (): number; }
>C : typeof C
>x : { (y: number): number; (): number; }
>call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
>C : typeof C

C.x.call(1); // ok (not an error because the `this` type of `x` is not constrained)
>C.x.call(1) : number
>C.x.call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
>C.x : { (y: number): number; (): number; }
>C : typeof C
>x : { (y: number): number; (): number; }
>call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
>1 : 1

18 changes: 18 additions & 0 deletions tests/cases/compiler/overloadedGenericInference.ts
58C7
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @target: es6
// @strict: true
// #35501
declare function fn(x: string): string;
declare function fn(x: string[]): string;

declare function map<A, R>(fn: (item: A) => R, list: A[]): R[];

const mapped = map(fn, ['1']);

// #30294 (partial fix)
declare class C {
static x(y: number): number;
static x(): number;
}
C.x.call(C, 1); // ok
C.x.call(C); // ok
C.x.call(1); // ok (not an error because the `this` type of `x` is not constrained)
0