Skip to content

Commit 601f5f2

Browse files
add granularConst flag
1 parent d03d107 commit 601f5f2

File tree

21 files changed

+382
-79
lines changed

21 files changed

+382
-79
lines changed

doc/spec.md

Lines changed: 5 additions & 5 deletions
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.

src/compiler/checker.ts

Lines changed: 79 additions & 74 deletions
Large diffs are not rendered by default.

src/compiler/commandLineParser.ts

Lines changed: 7 additions & 0 deletions
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: "granularConst",
274+
type: "boolean",
275+
showInSimplifiedHelpView: true,
276+
category: Diagnostics.Strict_Type_Checking_Options,
277+
description: Diagnostics.Enable_granular_type_inference_using_const
278+
},
272279
{
273280
name: "noImplicitThis",
274281
type: "boolean",

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3302,6 +3302,10 @@
33023302
"category": "Message",
33033303
"code": 6185
33043304
},
3305+
"Enable granular type inference using `const`.": {
3306+
"category": "Message",
3307+
"code": 6186
3308+
},
33053309
"Variable '{0}' implicitly has an '{1}' type.": {
33063310
"category": "Error",
33073311
"code": 7005

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
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+
granularConst?: boolean;
36253626
/* @internal */ stripInternal?: boolean;
36263627
suppressExcessPropertyErrors?: boolean;
36273628
suppressImplicitAnyIndexErrors?: boolean;

src/harness/unittests/configurationExtension.ts

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

src/harness/unittests/transpile.ts

Lines changed: 4 additions & 0 deletions
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 'granularConst'", "x;", {
417+
options: { compilerOptions: { granularConst: 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
});

src/server/protocol.ts

Lines changed: 1 addition & 0 deletions
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+
granularConst?: boolean;
24512452
suppressExcessPropertyErrors?: boolean;
24522453
suppressImplicitAnyIndexErrors?: boolean;
24532454
target?: ScriptTarget | ts.ScriptTarget;
Lines changed: 41 additions & 0 deletions
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);
Lines changed: 79 additions & 0 deletions
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+

0 commit comments

Comments
 (0)