Skip to content

Commit c85ba7b

Browse files
add widenTypes flag
1 parent d03d107 commit c85ba7b

File tree

22 files changed

+385
-79
lines changed

22 files changed

+385
-79
lines changed

Diff for: doc/spec.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function f() {
265265

266266
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.
267267

268-
  ![](images/image1.png)
268+
  ![](images/image1.png)
269269

270270
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.
271271

@@ -413,7 +413,7 @@ This signature denotes that a function may be passed as the parameter of the '$'
413413
414414
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.
415415
416-
  ![](images/image2.png)
416+
  ![](images/image2.png)
417417
418418
Section [3.3](#3.3) provides additional information about object types.
419419
@@ -630,7 +630,7 @@ An important goal of TypeScript is to provide accurate and straightforward types
630630

631631
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.
632632

633-
  ![](images/image3.png)
633+
  ![](images/image3.png)
634634

635635
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'.
636636

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

642642
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.
643643

644-
  ![](images/image4.png)
644+
  ![](images/image4.png)
645645

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

@@ -2630,7 +2630,7 @@ Each element expression in a non-empty array literal is processed as follows:
26302630
26312631
The resulting type an array literal expression is determined as follows:
26322632
2633-
* If the array literal is empty, the resulting type is an array type with the element type Undefined.
2633+
* If the array literal is empty, the resulting type is an empty tuple type in `granularConst` mode, otherwise an array type with the element type Undefined.
26342634
* 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.
26352635
* 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.
26362636
* 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.

Diff for: src/compiler/checker.ts

+80-74
Large diffs are not rendered by default.

Diff for: src/compiler/commandLineParser.ts

+7
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ namespace ts {
269269
category: Diagnostics.Strict_Type_Checking_Options,
270270
description: Diagnostics.Enable_strict_null_checks
271271
},
272+
{
273+
name: "widenTypes",
274+
type: "boolean",
275+
showInSimplifiedHelpView: true,
276+
category: Diagnostics.Strict_Type_Checking_Options,
277+
description: Diagnostics.Automatically_widen_types_even_in_params_and_const
278+
},
272279
{
273280
name: "noImplicitThis",
274281
type: "boolean",

Diff for: src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -3302,6 +3302,10 @@
33023302
"category": "Message",
33033303
"code": 6185
33043304
},
3305+
"Automatically widen types even in params and `const`.": {
3306+
"category": "Message",
3307+
"code": 6186
3308+
},
33053309
"Variable '{0}' implicitly has an '{1}' type.": {
33063310
"category": "Error",
33073311
"code": 7005

Diff for: src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3622,6 +3622,7 @@ namespace ts {
36223622
sourceRoot?: string;
36233623
strict?: boolean;
36243624
strictNullChecks?: boolean; // Always combine with strict property
3625+
widenTypes?: boolean;
36253626
/* @internal */ stripInternal?: boolean;
36263627
suppressExcessPropertyErrors?: boolean;
36273628
suppressImplicitAnyIndexErrors?: boolean;

Diff for: src/harness/unittests/configurationExtension.ts

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ namespace ts {
1616
strictNullChecks: false
1717
}
1818
},
19+
"/dev/tsconfig.widenTypes.json": {
20+
extends: "./tsconfig",
21+
compilerOptions: {
22+
widenTypes: false
23+
}
24+
},
1925
"/dev/configs/base.json": {
2026
compilerOptions: {
2127
allowJs: true,

Diff for: src/harness/unittests/transpile.ts

+4
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ var x = 0;`, {
413413
options: { compilerOptions: { strictNullChecks: true }, fileName: "input.js", reportDiagnostics: true }
414414
});
415415

416+
transpilesCorrectly("Supports setting 'widenTypes'", "x;", {
417+
options: { compilerOptions: { widenTypes: true }, fileName: "input.js", reportDiagnostics: true }
418+
});
419+
416420
transpilesCorrectly("Supports setting 'stripInternal'", "x;", {
417421
options: { compilerOptions: { stripInternal: true }, fileName: "input.js", reportDiagnostics: true }
418422
});

Diff for: src/server/protocol.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,7 @@ namespace ts.server.protocol {
24482448
sourceRoot?: string;
24492449
strict?: boolean;
24502450
strictNullChecks?: boolean;
2451+
widenTypes?: boolean;
24512452
suppressExcessPropertyErrors?: boolean;
24522453
suppressImplicitAnyIndexErrors?: boolean;
24532454
target?: ScriptTarget | ts.ScriptTarget;

Diff for: tests/baselines/reference/dontWiden.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [dontWiden.ts]
2+
const c = [1, 'a'];
3+
const d = { a: 1, b: 'c' };
4+
5+
interface SomeInterface { a: boolean }
6+
declare function foo<T extends any[]>(arg: T): { hi: T };
7+
declare function boo<T extends number[]>(arg: T): { hi: T };
8+
declare function bar(arg: SomeInterface): void
9+
declare function baz(arg: [number, 2, 3 | number]): void
10+
declare function bag(arg: number[]): void
11+
12+
// As variable assignees
13+
const a: number[] = [1, 2, 3];
14+
const b: SomeInterface = {a: true};
15+
const e = [1, 2, 3];
16+
17+
// Same, but as arguments
18+
foo([1, 2, 3]);
19+
bar({a: true});
20+
baz([1, 2, 3]);
21+
bag([1, 2, 3]);
22+
bag(e);
23+
boo([1, 2, 3]);
24+
boo(e);
25+
26+
27+
//// [dontWiden.js]
28+
var c = [1, 'a'];
29+
var d = { a: 1, b: 'c' };
30+
// As variable assignees
31+
var a = [1, 2, 3];
32+
var b = { a: true };
33+
var e = [1, 2, 3];
34+
// Same, but as arguments
35+
foo([1, 2, 3]);
36+
bar({ a: true });
37+
baz([1, 2, 3]);
38+
bag([1, 2, 3]);
39+
bag(e);
40+
boo([1, 2, 3]);
41+
boo(e);

Diff for: tests/baselines/reference/dontWiden.symbols

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
=== tests/cases/compiler/dontWiden.ts ===
2+
const c = [1, 'a'];
3+
>c : Symbol(c, Decl(dontWiden.ts, 0, 5))
4+
5+
const d = { a: 1, b: 'c' };
6+
>d : Symbol(d, Decl(dontWiden.ts, 1, 5))
7+
>a : Symbol(a, Decl(dontWiden.ts, 1, 11))
8+
>b : Symbol(b, Decl(dontWiden.ts, 1, 17))
9+
10+
interface SomeInterface { a: boolean }
11+
>SomeInterface : Symbol(SomeInterface, Decl(dontWiden.ts, 1, 27))
12+
>a : Symbol(SomeInterface.a, Decl(dontWiden.ts, 3, 25))
13+
14+
declare function foo<T extends any[]>(arg: T): { hi: T };
15+
>foo : Symbol(foo, Decl(dontWiden.ts, 3, 38))
16+
>T : Symbol(T, Decl(dontWiden.ts, 4, 21))
17+
>arg : Symbol(arg, Decl(dontWiden.ts, 4, 38))
18+
>T : Symbol(T, Decl(dontWiden.ts, 4, 21))
19+
>hi : Symbol(hi, Decl(dontWiden.ts, 4, 48))
20+
>T : Symbol(T, Decl(dontWiden.ts, 4, 21))
21+
22+
declare function boo<T extends number[]>(arg: T): { hi: T };
23+
>boo : Symbol(boo, Decl(dontWiden.ts, 4, 57))
24+
>T : Symbol(T, Decl(dontWiden.ts, 5, 21))
25+
>arg : Symbol(arg, Decl(dontWiden.ts, 5, 41))
26+
>T : Symbol(T, Decl(dontWiden.ts, 5, 21))
27+
>hi : Symbol(hi, Decl(dontWiden.ts, 5, 51))
28+
>T : Symbol(T, Decl(dontWiden.ts, 5, 21))
29+
30+
declare function bar(arg: SomeInterface): void
31+
>bar : Symbol(bar, Decl(dontWiden.ts, 5, 60))
32+
>arg : Symbol(arg, Decl(dontWiden.ts, 6, 21))
33+
>SomeInterface : Symbol(SomeInterface, Decl(dontWiden.ts, 1, 27))
34+
35+
declare function baz(arg: [number, 2, 3 | number]): void
36+
>baz : Symbol(baz, Decl(dontWiden.ts, 6, 46))
37+
>arg : Symbol(arg, Decl(dontWiden.ts, 7, 21))
38+
39+
declare function bag(arg: number[]): void
40+
>bag : Symbol(bag, Decl(dontWiden.ts, 7, 56))
41+
>arg : Symbol(arg, Decl(dontWiden.ts, 8, 21))
42+
43+
// As variable assignees
44+
const a: number[] = [1, 2, 3];
45+
>a : Symbol(a, Decl(dontWiden.ts, 11, 5))
46+
47+
const b: SomeInterface = {a: true};
48+
>b : Symbol(b, Decl(dontWiden.ts, 12, 5))
49+
>SomeInterface : Symbol(SomeInterface, Decl(dontWiden.ts, 1, 27))
50+
>a : Symbol(a, Decl(dontWiden.ts, 12, 26))
51+
52+
const e = [1, 2, 3];
53+
>e : Symbol(e, Decl(dontWiden.ts, 13, 5))
54+
55+
// Same, but as arguments
56+
foo([1, 2, 3]);
57+
>foo : Symbol(foo, Decl(dontWiden.ts, 3, 38))
58+
59+
bar({a: true});
60+
>bar : Symbol(bar, Decl(dontWiden.ts, 5, 60))
61+
>a : Symbol(a, Decl(dontWiden.ts, 17, 5))
62+
63+
baz([1, 2, 3]);
64+
>baz : Symbol(baz, Decl(dontWiden.ts, 6, 46))
65+
66+
bag([1, 2, 3]);
67+
>bag : Symbol(bag, Decl(dontWiden.ts, 7, 56))
68+
69+
bag(e);
70+
>bag : Symbol(bag, Decl(dontWiden.ts, 7, 56))
71+
>e : Symbol(e, Decl(dontWiden.ts, 13, 5))
72+
73+
boo([1, 2, 3]);
74+
>boo : Symbol(boo, Decl(dontWiden.ts, 4, 57))
75+
76+
boo(e);
77+
>boo : Symbol(boo, Decl(dontWiden.ts, 4, 57))
78+
>e : Symbol(e, Decl(dontWiden.ts, 13, 5))
79+

Diff for: tests/baselines/reference/dontWiden.types

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
=== tests/cases/compiler/dontWiden.ts ===
2+
const c = [1, 'a'];
3+
>c : [1, "a"]
4+
>[1, 'a'] : [1, "a"]
5+
>1 : 1
6+
>'a' : "a"
7+
8+
const d = { a: 1, b: 'c' };
9+
>d : { a: 1; b: "c"; }
10+
>{ a: 1, b: 'c' } : { a: 1; b: "c"; }
11+
>a : number
12+
>1 : 1
13+
>b : string
14+
>'c' : "c"
15+
16+
interface SomeInterface { a: boolean }
17+
>SomeInterface : SomeInterface
18+
>a : boolean
19+
20+
declare function foo<T extends any[]>(arg: T): { hi: T };
21+
>foo : <T extends any[]>(arg: T) => { hi: T; }
22+
>T : T
23+
>arg : T
24+
>T : T
25+
>hi : T
26+
>T : T
27+
28+
declare function boo<T extends number[]>(arg: T): { hi: T };
29+
>boo : <T extends number[]>(arg: T) => { hi: T; }
30+
>T : T
31+
>arg : T
32+
>T : T
33+
>hi : T
34+
>T : T
35+
36+
declare function bar(arg: SomeInterface): void
37+
>bar : (arg: SomeInterface) => void
38+
>arg : SomeInterface
39+
>SomeInterface : SomeInterface
40+
41+
declare function baz(arg: [number, 2, 3 | number]): void
42+
>baz : (arg: [number, 2, number]) => void
43+
>arg : [number, 2, number]
44+
45+
declare function bag(arg: number[]): void
46+
>bag : (arg: number[]) => void
47+
>arg : number[]
48+
49+
// As variable assignees
50+
const a: number[] = [1, 2, 3];
51+
>a : number[]
52+
>[1, 2, 3] : number[]
53+
>1 : 1
54+
>2 : 2
55+
>3 : 3
56+
57+
const b: SomeInterface = {a: true};
58+
>b : SomeInterface
59+
>SomeInterface : SomeInterface
60+
>{a: true} : { a: true; }
61+
>a : boolean
62+
>true : true
63+
64+
const e = [1, 2, 3];
65+
>e : [1, 2, 3]
66+
>[1, 2, 3] : [1, 2, 3]
67+
>1 : 1
68+
>2 : 2
69+
>3 : 3
70+
71+
// Same, but as arguments
72+
foo([1, 2, 3]);
73+
>foo([1, 2, 3]) : { hi: [1, 2, 3]; }
74+
>foo : <T extends any[]>(arg: T) => { hi: T; }
75+
>[1, 2, 3] : [1, 2, 3]
76+
>1 : 1
77+
>2 : 2
78+
>3 : 3
79+
80+
bar({a: true});
81+
>bar({a: true}) : void
82+
>bar : (arg: SomeInterface) => void
83+
>{a: true} : { a: true; }
84+
>a : boolean
85+
>true : true
86+
87+
baz([1, 2, 3]);
88+
>baz([1, 2, 3]) : void
89+
>baz : (arg: [number, 2, number]) => void
90+
>[1, 2, 3] : [number, 2, number]
91+
>1 : 1
92+
>2 : 2
93+
>3 : 3
94+
95+
bag([1, 2, 3]);
96+
>bag([1, 2, 3]) : void
97+
>bag : (arg: number[]) => void
98+
>[1, 2, 3] : number[]
99+
>1 : 1
100+
>2 : 2
101+
>3 : 3
102+
103+
bag(e);
104+
>bag(e) : void
105+
>bag : (arg: number[]) => void
106+
>e : [1, 2, 3]
107+
108+
boo([1, 2, 3]);
109+
>boo([1, 2, 3]) : { hi: [1, 2, 3]; }
110+
>boo : <T extends number[]>(arg: T) => { hi: T; }
111+
>[1, 2, 3] : [1, 2, 3]
112+
>1 : 1
113+
>2 : 2
114+
>3 : 3
115+
116+
boo(e);
117+
>boo(e) : { hi: [1, 2, 3]; }
118+
>boo : <T extends number[]>(arg: T) => { hi: T; }
119+
>e : [1, 2, 3]
120+

Diff for: tests/baselines/reference/transpile/Supports setting granularConst.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: tests/baselines/reference/transpile/Supports setting widenTypes.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json

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

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json

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

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json

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

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json

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

0 commit comments

Comments
 (0)