-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11126 from Microsoft/nonWideningLiterals
Non-widening explicit literal types
- Loading branch information
Showing
48 changed files
with
1,448 additions
and
231 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//// [ambientConstLiterals.ts] | ||
|
||
function f<T>(x: T): T { | ||
return x; | ||
} | ||
|
||
enum E { A, B, C } | ||
|
||
const c1 = "abc"; | ||
const c2 = 123; | ||
const c3 = c1; | ||
const c4 = c2; | ||
const c5 = f(123); | ||
const c6 = f(-123); | ||
const c7 = true; | ||
const c8 = E.A; | ||
const c9 = { x: "abc" }; | ||
const c10 = [123]; | ||
const c11 = "abc" + "def"; | ||
const c12 = 123 + 456; | ||
const c13 = Math.random() > 0.5 ? "abc" : "def"; | ||
const c14 = Math.random() > 0.5 ? 123 : 456; | ||
|
||
//// [ambientConstLiterals.js] | ||
function f(x) { | ||
return x; | ||
} | ||
var E; | ||
(function (E) { | ||
E[E["A"] = 0] = "A"; | ||
E[E["B"] = 1] = "B"; | ||
E[E["C"] = 2] = "C"; | ||
})(E || (E = {})); | ||
var c1 = "abc"; | ||
var c2 = 123; | ||
var c3 = c1; | ||
var c4 = c2; | ||
var c5 = f(123); | ||
var c6 = f(-123); | ||
var c7 = true; | ||
var c8 = E.A; | ||
var c9 = { x: "abc" }; | ||
var c10 = [123]; | ||
var c11 = "abc" + "def"; | ||
var c12 = 123 + 456; | ||
var c13 = Math.random() > 0.5 ? "abc" : "def"; | ||
var c14 = Math.random() > 0.5 ? 123 : 456; | ||
|
||
|
||
//// [ambientConstLiterals.d.ts] | ||
declare function f<T>(x: T): T; | ||
declare enum E { | ||
A = 0, | ||
B = 1, | ||
C = 2, | ||
} | ||
declare const c1 = "abc"; | ||
declare const c2 = 123; | ||
declare const c3 = "abc"; | ||
declare const c4 = 123; | ||
declare const c5 = 123; | ||
declare const c6 = -123; | ||
declare const c7: boolean; | ||
declare const c8: E; | ||
declare const c9: { | ||
x: string; | ||
}; | ||
declare const c10: number[]; | ||
declare const c11: string; | ||
declare const c12: number; | ||
declare const c13: string; | ||
declare const c14: number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
=== tests/cases/compiler/ambientConstLiterals.ts === | ||
|
||
function f<T>(x: T): T { | ||
>f : Symbol(f, Decl(ambientConstLiterals.ts, 0, 0)) | ||
>T : Symbol(T, Decl(ambientConstLiterals.ts, 1, 11)) | ||
>x : Symbol(x, Decl(ambientConstLiterals.ts, 1, 14)) | ||
>T : Symbol(T, Decl(ambientConstLiterals.ts, 1, 11)) | ||
>T : Symbol(T, Decl(ambientConstLiterals.ts, 1, 11)) | ||
|
||
return x; | ||
>x : Symbol(x, Decl(ambientConstLiterals.ts, 1, 14)) | ||
} | ||
|
||
enum E { A, B, C } | ||
>E : Symbol(E, Decl(ambientConstLiterals.ts, 3, 1)) | ||
>A : Symbol(E.A, Decl(ambientConstLiterals.ts, 5, 8)) | ||
>B : Symbol(E.B, Decl(ambientConstLiterals.ts, 5, 11)) | ||
>C : Symbol(E.C, Decl(ambientConstLiterals.ts, 5, 14)) | ||
|
||
const c1 = "abc"; | ||
>c1 : Symbol(c1, Decl(ambientConstLiterals.ts, 7, 5)) | ||
|
||
const c2 = 123; | ||
>c2 : Symbol(c2, Decl(ambientConstLiterals.ts, 8, 5)) | ||
|
||
const c3 = c1; | ||
>c3 : Symbol(c3, Decl(ambientConstLiterals.ts, 9, 5)) | ||
>c1 : Symbol(c1, Decl(ambientConstLiterals.ts, 7, 5)) | ||
|
||
const c4 = c2; | ||
>c4 : Symbol(c4, Decl(ambientConstLiterals.ts, 10, 5)) | ||
>c2 : Symbol(c2, Decl(ambientConstLiterals.ts, 8, 5)) | ||
|
||
const c5 = f(123); | ||
>c5 : Symbol(c5, Decl(ambientConstLiterals.ts, 11, 5)) | ||
>f : Symbol(f, Decl(ambientConstLiterals.ts, 0, 0)) | ||
|
||
const c6 = f(-123); | ||
>c6 : Symbol(c6, Decl(ambientConstLiterals.ts, 12, 5)) | ||
>f : Symbol(f, Decl(ambientConstLiterals.ts, 0, 0)) | ||
|
||
const c7 = true; | ||
>c7 : Symbol(c7, Decl(ambientConstLiterals.ts, 13, 5)) | ||
|
||
const c8 = E.A; | ||
>c8 : Symbol(c8, Decl(ambientConstLiterals.ts, 14, 5)) | ||
>E.A : Symbol(E.A, Decl(ambientConstLiterals.ts, 5, 8)) | ||
>E : Symbol(E, Decl(ambientConstLiterals.ts, 3, 1)) | ||
>A : Symbol(E.A, Decl(ambientConstLiterals.ts, 5, 8)) | ||
|
||
const c9 = { x: "abc" }; | ||
>c9 : Symbol(c9, Decl(ambientConstLiterals.ts, 15, 5)) | ||
>x : Symbol(x, Decl(ambientConstLiterals.ts, 15, 12)) | ||
|
||
const c10 = [123]; | ||
>c10 : Symbol(c10, Decl(ambientConstLiterals.ts, 16, 5)) | ||
|
||
const c11 = "abc" + "def"; | ||
>c11 : Symbol(c11, Decl(ambientConstLiterals.ts, 17, 5)) | ||
|
||
const c12 = 123 + 456; | ||
>c12 : Symbol(c12, Decl(ambientConstLiterals.ts, 18, 5)) | ||
|
||
const c13 = Math.random() > 0.5 ? "abc" : "def"; | ||
>c13 : Symbol(c13, Decl(ambientConstLiterals.ts, 19, 5)) | ||
>Math.random : Symbol(Math.random, Decl(lib.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.d.ts, --, --)) | ||
|
||
const c14 = Math.random() > 0.5 ? 123 : 456; | ||
>c14 : Symbol(c14, Decl(ambientConstLiterals.ts, 20, 5)) | ||
>Math.random : Symbol(Math.random, Decl(lib.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.d.ts, --, --)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
=== tests/cases/compiler/ambientConstLiterals.ts === | ||
|
||
function f<T>(x: T): T { | ||
>f : <T>(x: T) => T | ||
>T : T | ||
>x : T | ||
>T : T | ||
>T : T | ||
|
||
return x; | ||
>x : T | ||
} | ||
|
||
enum E { A, B, C } | ||
>E : E | ||
>A : E.A | ||
>B : E.B | ||
>C : E.C | ||
|
||
const c1 = "abc"; | ||
>c1 : "abc" | ||
>"abc" : "abc" | ||
|
||
const c2 = 123; | ||
>c2 : 123 | ||
>123 : 123 | ||
|
||
const c3 = c1; | ||
>c3 : "abc" | ||
>c1 : "abc" | ||
|
||
const c4 = c2; | ||
>c4 : 123 | ||
>c2 : 123 | ||
|
||
const c5 = f(123); | ||
>c5 : 123 | ||
>f(123) : 123 | ||
>f : <T>(x: T) => T | ||
>123 : 123 | ||
|
||
const c6 = f(-123); | ||
>c6 : -123 | ||
>f(-123) : -123 | ||
>f : <T>(x: T) => T | ||
>-123 : -123 | ||
>123 : 123 | ||
|
||
const c7 = true; | ||
>c7 : true | ||
>true : true | ||
|
||
const c8 = E.A; | ||
>c8 : E.A | ||
>E.A : E.A | ||
>E : typeof E | ||
>A : E.A | ||
|
||
const c9 = { x: "abc" }; | ||
>c9 : { x: string; } | ||
>{ x: "abc" } : { x: string; } | ||
>x : string | ||
>"abc" : "abc" | ||
|
||
const c10 = [123]; | ||
>c10 : number[] | ||
>[123] : number[] | ||
>123 : 123 | ||
|
||
const c11 = "abc" + "def"; | ||
>c11 : string | ||
>"abc" + "def" : string | ||
>"abc" : "abc" | ||
>"def" : "def" | ||
|
||
const c12 = 123 + 456; | ||
>c12 : number | ||
>123 + 456 : number | ||
>123 : 123 | ||
>456 : 456 | ||
|
||
const c13 = Math.random() > 0.5 ? "abc" : "def"; | ||
>c13 : "abc" | "def" | ||
>Math.random() > 0.5 ? "abc" : "def" : "abc" | "def" | ||
>Math.random() > 0.5 : boolean | ||
>Math.random() : number | ||
>Math.random : () => number | ||
>Math : Math | ||
>random : () => number | ||
>0.5 : 0.5 | ||
>"abc" : "abc" | ||
>"def" : "def" | ||
|
||
const c14 = Math.random() > 0.5 ? 123 : 456; | ||
>c14 : 123 | 456 | ||
>Math.random() > 0.5 ? 123 : 456 : 123 | 456 | ||
>Math.random() > 0.5 : boolean | ||
>Math.random() : number | ||
>Math.random : () => number | ||
>Math : Math | ||
>random : () => number | ||
>0.5 : 0.5 | ||
>123 : 123 | ||
>456 : 456 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
tests/cases/compiler/conditionalExpression1.ts(1,5): error TS2322: Type '"" | 1' is not assignable to type 'boolean'. | ||
Type '""' is not assignable to type 'boolean'. | ||
tests/cases/compiler/conditionalExpression1.ts(1,5): error TS2322: Type '1 | ""' is not assignable to type 'boolean'. | ||
Type '1' is not assignable to type 'boolean'. | ||
|
||
|
||
==== tests/cases/compiler/conditionalExpression1.ts (1 errors) ==== | ||
var x: boolean = (true ? 1 : ""); // should be an error | ||
~ | ||
!!! error TS2322: Type '"" | 1' is not assignable to type 'boolean'. | ||
!!! error TS2322: Type '""' is not assignable to type 'boolean'. | ||
!!! error TS2322: Type '1 | ""' is not assignable to type 'boolean'. | ||
!!! error TS2322: Type '1' is not assignable to type 'boolean'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.