Skip to content

Commit 0c131fa

Browse files
authored
Merge pull request #9407 from Microsoft/literalTypes
Number, enum, and boolean literal types
2 parents 0783743 + 5ff07dc commit 0c131fa

File tree

442 files changed

+13793
-4854
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

442 files changed

+13793
-4854
lines changed

src/compiler/binder.ts

+4-18
Original file line numberDiff line numberDiff line change
@@ -618,18 +618,10 @@ namespace ts {
618618
return false;
619619
}
620620

621-
function isNarrowingNullCheckOperands(expr1: Expression, expr2: Expression) {
622-
return (expr1.kind === SyntaxKind.NullKeyword || expr1.kind === SyntaxKind.Identifier && (<Identifier>expr1).text === "undefined") && isNarrowableOperand(expr2);
623-
}
624-
625621
function isNarrowingTypeofOperands(expr1: Expression, expr2: Expression) {
626622
return expr1.kind === SyntaxKind.TypeOfExpression && isNarrowableOperand((<TypeOfExpression>expr1).expression) && expr2.kind === SyntaxKind.StringLiteral;
627623
}
628624

629-
function isNarrowingDiscriminant(expr: Expression) {
630-
return expr.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((<PropertyAccessExpression>expr).expression);
631-
}
632-
633625
function isNarrowingBinaryExpression(expr: BinaryExpression) {
634626
switch (expr.operatorToken.kind) {
635627
case SyntaxKind.EqualsToken:
@@ -638,9 +630,8 @@ namespace ts {
638630
case SyntaxKind.ExclamationEqualsToken:
639631
case SyntaxKind.EqualsEqualsEqualsToken:
640632
case SyntaxKind.ExclamationEqualsEqualsToken:
641-
return isNarrowingNullCheckOperands(expr.right, expr.left) || isNarrowingNullCheckOperands(expr.left, expr.right) ||
642-
isNarrowingTypeofOperands(expr.right, expr.left) || isNarrowingTypeofOperands(expr.left, expr.right) ||
643-
isNarrowingDiscriminant(expr.left) || isNarrowingDiscriminant(expr.right);
633+
return isNarrowableOperand(expr.left) || isNarrowableOperand(expr.right) ||
634+
isNarrowingTypeofOperands(expr.right, expr.left) || isNarrowingTypeofOperands(expr.left, expr.right);
644635
case SyntaxKind.InstanceOfKeyword:
645636
return isNarrowableOperand(expr.left);
646637
case SyntaxKind.CommaToken:
@@ -664,11 +655,6 @@ namespace ts {
664655
return isNarrowableReference(expr);
665656
}
666657

667-
function isNarrowingSwitchStatement(switchStatement: SwitchStatement) {
668-
const expr = switchStatement.expression;
669-
return expr.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((<PropertyAccessExpression>expr).expression);
670-
}
671-
672658
function createBranchLabel(): FlowLabel {
673659
return {
674660
flags: FlowFlags.BranchLabel,
@@ -718,7 +704,7 @@ namespace ts {
718704
}
719705

720706
function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode {
721-
if (!isNarrowingSwitchStatement(switchStatement)) {
707+
if (!isNarrowingExpression(switchStatement.expression)) {
722708
return antecedent;
723709
}
724710
setFlowNodeReferenced(antecedent);
@@ -1983,7 +1969,7 @@ namespace ts {
19831969
function bindThisPropertyAssignment(node: BinaryExpression) {
19841970
// Declare a 'member' in case it turns out the container was an ES5 class or ES6 constructor
19851971
let assignee: Node;
1986-
if (container.kind === SyntaxKind.FunctionDeclaration || container.kind === SyntaxKind.FunctionDeclaration) {
1972+
if (container.kind === SyntaxKind.FunctionDeclaration || container.kind === SyntaxKind.FunctionExpression) {
19871973
assignee = container;
19881974
}
19891975
else if (container.kind === SyntaxKind.Constructor) {

src/compiler/checker.ts

+734-377
Large diffs are not rendered by default.

src/compiler/core.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ namespace ts {
9393
return undefined;
9494
}
9595

96-
export function contains<T>(array: T[], value: T, areEqual?: (a: T, b: T) => boolean): boolean {
96+
export function contains<T>(array: T[], value: T): boolean {
9797
if (array) {
9898
for (const v of array) {
99-
if (areEqual ? areEqual(v, value) : v === value) {
99+
if (v === value) {
100100
return true;
101101
}
102102
}
@@ -182,10 +182,13 @@ namespace ts {
182182
let result: T[];
183183
if (array) {
184184
result = [];
185-
for (const item of array) {
186-
if (!contains(result, item, areEqual)) {
187-
result.push(item);
185+
loop: for (const item of array) {
186+
for (const res of result) {
187+
if (areEqual ? areEqual(res, item) : res === item) {
188+
continue loop;
189+
}
188190
}
191+
result.push(item);
189192
}
190193
}
191194
return result;

src/compiler/declarationEmitter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ namespace ts {
397397
case SyntaxKind.NullKeyword:
398398
case SyntaxKind.NeverKeyword:
399399
case SyntaxKind.ThisType:
400-
case SyntaxKind.StringLiteralType:
400+
case SyntaxKind.LiteralType:
401401
return writeTextOfNode(currentText, type);
402402
case SyntaxKind.ExpressionWithTypeArguments:
403403
return emitExpressionWithTypeArguments(<ExpressionWithTypeArguments>type);

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,10 @@
17551755
"category": "Error",
17561756
"code": 2534
17571757
},
1758+
"Enum type '{0}' has members with initializers that are not literals.": {
1759+
"category": "Error",
1760+
"code": 2535
1761+
},
17581762
"JSX element attributes type '{0}' may not be a union type.": {
17591763
"category": "Error",
17601764
"code": 2600

src/compiler/emitter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6045,7 +6045,7 @@ const _super = (function (geti, seti) {
60456045
return;
60466046

60476047
case SyntaxKind.StringKeyword:
6048-
case SyntaxKind.StringLiteralType:
6048+
case SyntaxKind.LiteralType:
60496049
write("String");
60506050
return;
60516051

0 commit comments

Comments
 (0)