-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Strict function types #18654
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
Strict function types #18654
Changes from 17 commits
12f5dd8
f8ff7f7
670d711
a0fa69f
b58e0fb
84f7afd
54eadef
44cc8c5
dd466ae
24698dd
f8e2cc1
589e1f4
afc8a26
70e8f73
91691f6
6a481e8
1795614
5613be4
1609196
0756aa1
c626d9d
936f98d
bf75a3f
bff843a
c2344e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3215,6 +3215,7 @@ namespace ts { | |
NonPrimitive = 1 << 24, // intrinsic object type | ||
/* @internal */ | ||
JsxAttributes = 1 << 25, // Jsx attributes type | ||
MarkerType = 1 << 26, // Marker type used for variance probing | ||
|
||
/* @internal */ | ||
Nullable = Undefined | Null, | ||
|
@@ -3343,10 +3344,19 @@ namespace ts { | |
typeArguments?: Type[]; // Type reference type arguments (undefined if none) | ||
} | ||
|
||
export const enum Variance { | ||
Invariant = 0, // Neither covariant nor contravariant | ||
Covariant = 1, // Covariant | ||
Contravariant = 2, // Contravariant | ||
Bivariant = 3, // Both covariant and contravariant | ||
Independent = 4, // Unwitnessed type parameter | ||
} | ||
|
||
// Generic class and interface types | ||
export interface GenericType extends InterfaceType, TypeReference { | ||
/* @internal */ | ||
instantiations: Map<TypeReference>; // Generic instantiation cache | ||
instantiations: Map<TypeReference>; // Generic instantiation cache | ||
variances?: Variance[]; // Variance of each type parameter | ||
} | ||
|
||
export interface UnionOrIntersectionType extends Type { | ||
|
@@ -3522,9 +3532,10 @@ namespace ts { | |
} | ||
|
||
export const enum InferencePriority { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should probably be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably, but not really related to this PR since the type already existed before. I'm going to leave it be for now. |
||
NakedTypeVariable = 1 << 0, // Naked type variable in union or intersection type | ||
MappedType = 1 << 1, // Reverse inference for mapped type | ||
ReturnType = 1 << 2, // Inference made from return type of generic function | ||
Contravariant = 1 << 0, // Inference from contravariant position | ||
NakedTypeVariable = 1 << 1, // Naked type variable in union or intersection type | ||
MappedType = 1 << 2, // Reverse inference for mapped type | ||
ReturnType = 1 << 3, // Inference made from return type of generic function | ||
} | ||
|
||
export interface InferenceInfo { | ||
|
@@ -3707,6 +3718,7 @@ namespace ts { | |
sourceMap?: boolean; | ||
sourceRoot?: string; | ||
strict?: boolean; | ||
strictFunctionTypes?: boolean; // Always combine with strict property | ||
strictNullChecks?: boolean; // Always combine with strict property | ||
/* @internal */ stripInternal?: boolean; | ||
suppressExcessPropertyErrors?: boolean; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//// [strictFunctionTypes1.ts] | ||
declare function f1<T>(f1: (x: T) => void, f2: (x: T) => void): (x: T) => void; | ||
declare function f2<T>(obj: T, f1: (x: T) => void, f2: (x: T) => void): T; | ||
declare function f3<T>(obj: T, f1: (x: T) => void, f2: (f: (x: T) => void) => void): T; | ||
|
||
interface Func<T> { (x: T): void } | ||
|
||
declare function f4<T>(f1: Func<T>, f2: Func<T>): Func<T>; | ||
|
||
declare function fo(x: Object): void; | ||
declare function fs(x: string): void; | ||
declare function fx(f: (x: "def") => void): void; | ||
|
||
const x1 = f1(fo, fs); // (x: string) => void | ||
const x2 = f2("abc", fo, fs); // "abc" | ||
const x3 = f3("abc", fo, fx); // "abc" | "def" | ||
const x4 = f4(fo, fs); // Func<string> | ||
|
||
|
||
//// [strictFunctionTypes1.js] | ||
"use strict"; | ||
var x1 = f1(fo, fs); // (x: string) => void | ||
var x2 = f2("abc", fo, fs); // "abc" | ||
var x3 = f3("abc", fo, fx); // "abc" | "def" | ||
var x4 = f4(fo, fs); // Func<string> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
=== tests/cases/compiler/strictFunctionTypes1.ts === | ||
declare function f1<T>(f1: (x: T) => void, f2: (x: T) => void): (x: T) => void; | ||
>f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 0, 20)) | ||
>f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 0, 23)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 0, 28)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 0, 20)) | ||
>f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 0, 42)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 0, 48)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 0, 20)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 0, 65)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 0, 20)) | ||
|
||
declare function f2<T>(obj: T, f1: (x: T) => void, f2: (x: T) => void): T; | ||
>f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 0, 79)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 1, 20)) | ||
>obj : Symbol(obj, Decl(strictFunctionTypes1.ts, 1, 23)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 1, 20)) | ||
>f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 1, 30)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 1, 36)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 1, 20)) | ||
>f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 1, 50)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 1, 56)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 1, 20)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 1, 20)) | ||
|
||
declare function f3<T>(obj: T, f1: (x: T) => void, f2: (f: (x: T) => void) => void): T; | ||
>f3 : Symbol(f3, Decl(strictFunctionTypes1.ts, 1, 74)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 2, 20)) | ||
>obj : Symbol(obj, Decl(strictFunctionTypes1.ts, 2, 23)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 2, 20)) | ||
>f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 2, 30)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 2, 36)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 2, 20)) | ||
>f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 2, 50)) | ||
>f : Symbol(f, Decl(strictFunctionTypes1.ts, 2, 56)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 2, 60)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 2, 20)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 2, 20)) | ||
|
||
interface Func<T> { (x: T): void } | ||
>Func : Symbol(Func, Decl(strictFunctionTypes1.ts, 2, 87)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 4, 15)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 4, 21)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 4, 15)) | ||
|
||
declare function f4<T>(f1: Func<T>, f2: Func<T>): Func<T>; | ||
>f4 : Symbol(f4, Decl(strictFunctionTypes1.ts, 4, 34)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 6, 20)) | ||
>f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 6, 23)) | ||
>Func : Symbol(Func, Decl(strictFunctionTypes1.ts, 2, 87)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 6, 20)) | ||
>f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 6, 35)) | ||
>Func : Symbol(Func, Decl(strictFunctionTypes1.ts, 2, 87)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 6, 20)) | ||
>Func : Symbol(Func, Decl(strictFunctionTypes1.ts, 2, 87)) | ||
>T : Symbol(T, Decl(strictFunctionTypes1.ts, 6, 20)) | ||
|
||
declare function fo(x: Object): void; | ||
>fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 8, 20)) | ||
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) | ||
|
||
declare function fs(x: string): void; | ||
>fs : Symbol(fs, Decl(strictFunctionTypes1.ts, 8, 37)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 9, 20)) | ||
|
||
declare function fx(f: (x: "def") => void): void; | ||
>fx : Symbol(fx, Decl(strictFunctionTypes1.ts, 9, 37)) | ||
>f : Symbol(f, Decl(strictFunctionTypes1.ts, 10, 20)) | ||
>x : Symbol(x, Decl(strictFunctionTypes1.ts, 10, 24)) | ||
|
||
const x1 = f1(fo, fs); // (x: string) => void | ||
>x1 : Symbol(x1, Decl(strictFunctionTypes1.ts, 12, 5)) | ||
>f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 0, 0)) | ||
>fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) | ||
>fs : Symbol(fs, Decl(strictFunctionTypes1.ts, 8, 37)) | ||
|
||
const x2 = f2("abc", fo, fs); // "abc" | ||
>x2 : Symbol(x2, Decl(strictFunctionTypes1.ts, 13, 5)) | ||
>f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 0, 79)) | ||
>fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) | ||
>fs : Symbol(fs, Decl(strictFunctionTypes1.ts, 8, 37)) | ||
|
||
const x3 = f3("abc", fo, fx); // "abc" | "def" | ||
>x3 : Symbol(x3, Decl(strictFunctionTypes1.ts, 14, 5)) | ||
>f3 : Symbol(f3, Decl(strictFunctionTypes1.ts, 1, 74)) | ||
>fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) | ||
>fx : Symbol(fx, Decl(strictFunctionTypes1.ts, 9, 37)) | ||
|
||
const x4 = f4(fo, fs); // Func<string> | ||
>x4 : Symbol(x4, Decl(strictFunctionTypes1.ts, 15, 5)) | ||
>f4 : Symbol(f4, Decl(strictFunctionTypes1.ts, 4, 34)) | ||
>fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) | ||
>fs : Symbol(fs, Decl(strictFunctionTypes1.ts, 8, 37)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be probably
@internal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed