Skip to content
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

add compiler flag to enable granular inference outside var/let #17785

Closed
Closed
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
10 changes: 5 additions & 5 deletions doc/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function f() {

To benefit from this inference, a programmer can use the TypeScript language service. For example, a code editor can incorporate the TypeScript language service and use the service to find the members of a string object as in the following screen shot.

  ![](images/image1.png)
  ![](images/image1.png)

In this example, the programmer benefits from type inference without providing type annotations. Some beneficial tools, however, do require the programmer to provide type annotations. In TypeScript, we can express a parameter requirement as in the following code fragment.

Expand Down Expand Up @@ -413,7 +413,7 @@ This signature denotes that a function may be passed as the parameter of the '$'

A typical client would not need to add any additional typing but could just use a community-supplied typing to discover (through statement completion with documentation tips) and verify (through static checking) correct use of the library, as in the following screen shot.

  ![](images/image2.png)
  ![](images/image2.png)

Section [3.3](#3.3) provides additional information about object types.

Expand Down Expand Up @@ -630,7 +630,7 @@ An important goal of TypeScript is to provide accurate and straightforward types

JavaScript programming interfaces often include functions whose behavior is discriminated by a string constant passed to the function. The Document Object Model makes heavy use of this pattern. For example, the following screen shot shows that the 'createElement' method of the 'document' object has multiple signatures, some of which identify the types returned when specific strings are passed into the method.

  ![](images/image3.png)
  ![](images/image3.png)

The following code fragment uses this feature. Because the 'span' variable is inferred to have the type 'HTMLSpanElement', the code can reference without static error the 'isMultiline' property of 'span'.

Expand All @@ -641,7 +641,7 @@ span.isMultiLine = false; // OK: HTMLSpanElement has isMultiline property

In the following screen shot, a programming tool combines information from overloading on string parameters with contextual typing to infer that the type of the variable 'e' is 'MouseEvent' and that therefore 'e' has a 'clientX' property.

  ![](images/image4.png)
  ![](images/image4.png)

Section [3.9.2.4](#3.9.2.4) provides details on how to use string literals in function signatures.

Expand Down Expand Up @@ -2630,7 +2630,7 @@ Each element expression in a non-empty array literal is processed as follows:

The resulting type an array literal expression is determined as follows:

* If the array literal is empty, the resulting type is an array type with the element type Undefined.
* If the array literal is empty, the resulting type is an empty tuple type if `widenTypes` is disabled, otherwise an array type with the element type Undefined.
* Otherwise, if the array literal contains no spread elements and is contextually typed by a tuple-like type (section [3.3.3](#3.3.3)), the resulting type is a tuple type constructed from the types of the element expressions.
* Otherwise, if the array literal contains no spread elements and is an array assignment pattern in a destructuring assignment (section [4.21.1](#4.21.1)), the resulting type is a tuple type constructed from the types of the element expressions.
* Otherwise, the resulting type is an array type with an element type that is the union of the types of the non-spread element expressions and the numeric index signature types of the spread element expressions.
Expand Down
154 changes: 80 additions & 74 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ namespace ts {
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Enable_strict_null_checks
},
{
name: "widenTypes",
type: "boolean",
showInSimplifiedHelpView: true,
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Automatically_widen_types_even_in_params_and_const
},
{
name: "noImplicitThis",
type: "boolean",
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3302,6 +3302,10 @@
"category": "Message",
"code": 6185
},
"Automatically widen types even in params and `const`.": {
"category": "Message",
"code": 6186
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7005
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3622,6 +3622,7 @@ namespace ts {
sourceRoot?: string;
strict?: boolean;
strictNullChecks?: boolean; // Always combine with strict property
widenTypes?: boolean;
/* @internal */ stripInternal?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
Expand Down
6 changes: 6 additions & 0 deletions src/harness/unittests/configurationExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ namespace ts {
strictNullChecks: false
}
},
"/dev/tsconfig.widenTypes.json": {
extends: "./tsconfig",
compilerOptions: {
widenTypes: false
}
},
"/dev/configs/base.json": {
compilerOptions: {
allowJs: true,
Expand Down
4 changes: 4 additions & 0 deletions src/harness/unittests/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@ var x = 0;`, {
options: { compilerOptions: { strictNullChecks: true }, fileName: "input.js", reportDiagnostics: true }
});

transpilesCorrectly("Supports setting 'widenTypes'", "x;", {
options: { compilerOptions: { widenTypes: true }, fileName: "input.js", reportDiagnostics: true }
});

transpilesCorrectly("Supports setting 'stripInternal'", "x;", {
options: { compilerOptions: { stripInternal: true }, fileName: "input.js", reportDiagnostics: true }
});
Expand Down
1 change: 1 addition & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2448,6 +2448,7 @@ namespace ts.server.protocol {
sourceRoot?: string;
strict?: boolean;
strictNullChecks?: boolean;
widenTypes?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
target?: ScriptTarget | ts.ScriptTarget;
Expand Down
41 changes: 41 additions & 0 deletions tests/baselines/reference/dontWiden.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [dontWiden.ts]
const c = [1, 'a'];
const d = { a: 1, b: 'c' };

interface SomeInterface { a: boolean }
declare function foo<T extends any[]>(arg: T): { hi: T };
declare function boo<T extends number[]>(arg: T): { hi: T };
declare function bar(arg: SomeInterface): void
declare function baz(arg: [number, 2, 3 | number]): void
declare function bag(arg: number[]): void

// As variable assignees
const a: number[] = [1, 2, 3];
const b: SomeInterface = {a: true};
const e = [1, 2, 3];

// Same, but as arguments
foo([1, 2, 3]);
bar({a: true});
baz([1, 2, 3]);
bag([1, 2, 3]);
bag(e);
boo([1, 2, 3]);
boo(e);


//// [dontWiden.js]
var c = [1, 'a'];
var d = { a: 1, b: 'c' };
// As variable assignees
var a = [1, 2, 3];
var b = { a: true };
var e = [1, 2, 3];
// Same, but as arguments
foo([1, 2, 3]);
bar({ a: true });
baz([1, 2, 3]);
bag([1, 2, 3]);
bag(e);
boo([1, 2, 3]);
boo(e);
79 changes: 79 additions & 0 deletions tests/baselines/reference/dontWiden.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
=== tests/cases/compiler/dontWiden.ts ===
const c = [1, 'a'];
>c : Symbol(c, Decl(dontWiden.ts, 0, 5))

const d = { a: 1, b: 'c' };
>d : Symbol(d, Decl(dontWiden.ts, 1, 5))
>a : Symbol(a, Decl(dontWiden.ts, 1, 11))
>b : Symbol(b, Decl(dontWiden.ts, 1, 17))

interface SomeInterface { a: boolean }
>SomeInterface : Symbol(SomeInterface, Decl(dontWiden.ts, 1, 27))
>a : Symbol(SomeInterface.a, Decl(dontWiden.ts, 3, 25))

declare function foo<T extends any[]>(arg: T): { hi: T };
>foo : Symbol(foo, Decl(dontWiden.ts, 3, 38))
>T : Symbol(T, Decl(dontWiden.ts, 4, 21))
>arg : Symbol(arg, Decl(dontWiden.ts, 4, 38))
>T : Symbol(T, Decl(dontWiden.ts, 4, 21))
>hi : Symbol(hi, Decl(dontWiden.ts, 4, 48))
>T : Symbol(T, Decl(dontWiden.ts, 4, 21))

declare function boo<T extends number[]>(arg: T): { hi: T };
>boo : Symbol(boo, Decl(dontWiden.ts, 4, 57))
>T : Symbol(T, Decl(dontWiden.ts, 5, 21))
>arg : Symbol(arg, Decl(dontWiden.ts, 5, 41))
>T : Symbol(T, Decl(dontWiden.ts, 5, 21))
>hi : Symbol(hi, Decl(dontWiden.ts, 5, 51))
>T : Symbol(T, Decl(dontWiden.ts, 5, 21))

declare function bar(arg: SomeInterface): void
>bar : Symbol(bar, Decl(dontWiden.ts, 5, 60))
>arg : Symbol(arg, Decl(dontWiden.ts, 6, 21))
>SomeInterface : Symbol(SomeInterface, Decl(dontWiden.ts, 1, 27))

declare function baz(arg: [number, 2, 3 | number]): void
>baz : Symbol(baz, Decl(dontWiden.ts, 6, 46))
>arg : Symbol(arg, Decl(dontWiden.ts, 7, 21))

declare function bag(arg: number[]): void
>bag : Symbol(bag, Decl(dontWiden.ts, 7, 56))
>arg : Symbol(arg, Decl(dontWiden.ts, 8, 21))

// As variable assignees
const a: number[] = [1, 2, 3];
>a : Symbol(a, Decl(dontWiden.ts, 11, 5))

const b: SomeInterface = {a: true};
>b : Symbol(b, Decl(dontWiden.ts, 12, 5))
>SomeInterface : Symbol(SomeInterface, Decl(dontWiden.ts, 1, 27))
>a : Symbol(a, Decl(dontWiden.ts, 12, 26))

const e = [1, 2, 3];
>e : Symbol(e, Decl(dontWiden.ts, 13, 5))

// Same, but as arguments
foo([1, 2, 3]);
>foo : Symbol(foo, Decl(dontWiden.ts, 3, 38))

bar({a: true});
>bar : Symbol(bar, Decl(dontWiden.ts, 5, 60))
>a : Symbol(a, Decl(dontWiden.ts, 17, 5))

baz([1, 2, 3]);
>baz : Symbol(baz, Decl(dontWiden.ts, 6, 46))

bag([1, 2, 3]);
>bag : Symbol(bag, Decl(dontWiden.ts, 7, 56))

bag(e);
>bag : Symbol(bag, Decl(dontWiden.ts, 7, 56))
>e : Symbol(e, Decl(dontWiden.ts, 13, 5))

boo([1, 2, 3]);
>boo : Symbol(boo, Decl(dontWiden.ts, 4, 57))

boo(e);
>boo : Symbol(boo, Decl(dontWiden.ts, 4, 57))
>e : Symbol(e, Decl(dontWiden.ts, 13, 5))

120 changes: 120 additions & 0 deletions tests/baselines/reference/dontWiden.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
=== tests/cases/compiler/dontWiden.ts ===
const c = [1, 'a'];
>c : [1, "a"]
>[1, 'a'] : [1, "a"]
>1 : 1
>'a' : "a"

const d = { a: 1, b: 'c' };
>d : { a: 1; b: "c"; }
>{ a: 1, b: 'c' } : { a: 1; b: "c"; }
>a : number
>1 : 1
>b : string
>'c' : "c"

interface SomeInterface { a: boolean }
>SomeInterface : SomeInterface
>a : boolean

declare function foo<T extends any[]>(arg: T): { hi: T };
>foo : <T extends any[]>(arg: T) => { hi: T; }
>T : T
>arg : T
>T : T
>hi : T
>T : T

declare function boo<T extends number[]>(arg: T): { hi: T };
>boo : <T extends number[]>(arg: T) => { hi: T; }
>T : T
>arg : T
>T : T
>hi : T
>T : T

declare function bar(arg: SomeInterface): void
>bar : (arg: SomeInterface) => void
>arg : SomeInterface
>SomeInterface : SomeInterface

declare function baz(arg: [number, 2, 3 | number]): void
>baz : (arg: [number, 2, number]) => void
>arg : [number, 2, number]

declare function bag(arg: number[]): void
>bag : (arg: number[]) => void
>arg : number[]

// As variable assignees
const a: number[] = [1, 2, 3];
>a : number[]
>[1, 2, 3] : number[]
>1 : 1
>2 : 2
>3 : 3

const b: SomeInterface = {a: true};
>b : SomeInterface
>SomeInterface : SomeInterface
>{a: true} : { a: true; }
>a : boolean
>true : true

const e = [1, 2, 3];
>e : [1, 2, 3]
>[1, 2, 3] : [1, 2, 3]
>1 : 1
>2 : 2
>3 : 3

// Same, but as arguments
foo([1, 2, 3]);
>foo([1, 2, 3]) : { hi: [1, 2, 3]; }
>foo : <T extends any[]>(arg: T) => { hi: T; }
>[1, 2, 3] : [1, 2, 3]
>1 : 1
>2 : 2
>3 : 3

bar({a: true});
>bar({a: true}) : void
>bar : (arg: SomeInterface) => void
>{a: true} : { a: true; }
>a : boolean
>true : true

baz([1, 2, 3]);
>baz([1, 2, 3]) : void
>baz : (arg: [number, 2, number]) => void
>[1, 2, 3] : [number, 2, number]
>1 : 1
>2 : 2
>3 : 3

bag([1, 2, 3]);
>bag([1, 2, 3]) : void
>bag : (arg: number[]) => void
>[1, 2, 3] : number[]
>1 : 1
>2 : 2
>3 : 3

bag(e);
>bag(e) : void
>bag : (arg: number[]) => void
>e : [1, 2, 3]

boo([1, 2, 3]);
>boo([1, 2, 3]) : { hi: [1, 2, 3]; }
>boo : <T extends number[]>(arg: T) => { hi: T; }
>[1, 2, 3] : [1, 2, 3]
>1 : 1
>2 : 2
>3 : 3

boo(e);
>boo(e) : { hi: [1, 2, 3]; }
>boo : <T extends number[]>(arg: T) => { hi: T; }
>e : [1, 2, 3]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"strict": true /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "widenTypes": true, /* Automatically widen types even in params and `const`. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "widenTypes": true, /* Automatically widen types even in params and `const`. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"strict": true /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "widenTypes": true, /* Automatically widen types even in params and `const`. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"strict": true /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "widenTypes": true, /* Automatically widen types even in params and `const`. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

Expand Down
Loading