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

Number, enum, and boolean literal types #9407

Merged
merged 58 commits into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
89596cb
Numeric and boolean literal types
ahejlsberg Jun 18, 2016
a204622
Accept new baselines
ahejlsberg Jun 20, 2016
a930917
Introduce literal type locations
ahejlsberg Jun 20, 2016
3ccd68b
Accept new baselines
ahejlsberg Jun 20, 2016
a91f714
Literal types for const enum members
ahejlsberg Jun 21, 2016
8455143
Accept new baselines
ahejlsberg Jun 21, 2016
4774896
Correct handling of falsy types ("" | 0 | false)
ahejlsberg Jun 24, 2016
770f423
Accept new baselines
ahejlsberg Jun 24, 2016
e900ebf
Make boolean assignable to union types containing both true and false
ahejlsberg Jun 25, 2016
85128e0
Narrow unions of literal types by equality and truthiness
ahejlsberg Jun 29, 2016
a2a4896
Accept new baselines
ahejlsberg Jun 29, 2016
e14fe5b
Remove unused functions
ahejlsberg Jun 29, 2016
70e2c43
Consider null, undefined, and void to be unit types in type guards
ahejlsberg Jun 29, 2016
d37d50a
Ensure const enum members with same value have same type identity
ahejlsberg Jul 1, 2016
a91a293
Accept new baselines
ahejlsberg Jul 1, 2016
881d510
Detect always truthy and always falsy types with &&, ||, ! operators
ahejlsberg Jul 2, 2016
1cec8dc
All types can be null/undefined and thus falsy in regular type checki…
ahejlsberg Jul 3, 2016
e8d4a5a
Accept new baselines
ahejlsberg Jul 3, 2016
5225997
Merge branch 'master' into literalTypes
ahejlsberg Jul 5, 2016
280b27e
Narrowing doesn't require switch expression to be unit type
ahejlsberg Jul 7, 2016
868d5e6
Merge branch 'master' into literalTypes
ahejlsberg Jul 7, 2016
82c26cd
Adding selected tests from #6196
ahejlsberg Jul 7, 2016
871aee1
Adding new tests
ahejlsberg Jul 7, 2016
6309ada
Accepting new baselines
ahejlsberg Jul 7, 2016
44339dd
Performance optimizations
ahejlsberg Jul 14, 2016
4501b3e
Accept new baselines
ahejlsberg Jul 14, 2016
b5a5758
No subtype reduction in createUnionOrIntersectionProperty for perform…
ahejlsberg Jul 16, 2016
01a33f7
Accept new baselines
ahejlsberg Jul 16, 2016
614d171
Include type parameter constrains in literal type context determination
ahejlsberg Jul 16, 2016
a96d38e
Accept new baselines
ahejlsberg Jul 16, 2016
cb27e54
Fix fourslash test
ahejlsberg Jul 18, 2016
c48cd4a
Unify 'boolean' and 'true | false'
ahejlsberg Jul 18, 2016
32f4cbb
Accept new baselines
ahejlsberg Jul 18, 2016
0f132cd
Order union type constituents by type ID
ahejlsberg Jul 19, 2016
15a6667
Fix fourslash tests
ahejlsberg Jul 19, 2016
7d1c2fc
Accept new baselines
ahejlsberg Jul 19, 2016
a17bd02
Associate type alias names with union, intersection and literal types
ahejlsberg Jul 19, 2016
a53a53f
Accept new baselines
ahejlsberg Jul 19, 2016
26713c8
Expand top level of declared type in type alias declaration
ahejlsberg Jul 19, 2016
a2c4176
Fix fourslash test
ahejlsberg Jul 19, 2016
1868f2e
Remove bizarre fourslash test
ahejlsberg Jul 19, 2016
f5f8a45
Optimize checkTypeRelatedTo
ahejlsberg Jul 20, 2016
451f48b
Optimize checkTypeRelatedTo, part 2
ahejlsberg Jul 21, 2016
afd39cc
Enum type is also a union of the literal enum types it declares
ahejlsberg Jul 22, 2016
8c64759
Fix bug in binder uncovered by changes
ahejlsberg Jul 22, 2016
9a23b11
Change parser to use token() function for accessing current token
ahejlsberg Jul 22, 2016
f7753af
Reduce unions of enum literal types when displaying types
ahejlsberg Jul 22, 2016
d3c91e0
Accept new baselines
ahejlsberg Jul 22, 2016
178883a
Fix issue in getTypeDefinitionAtPosition
ahejlsberg Jul 23, 2016
835645c
Fix fourslash test
ahejlsberg Jul 23, 2016
b70132a
Fix linting errors
ahejlsberg Jul 23, 2016
60cc5df
Change getUnionType to default to no subtype reduction
ahejlsberg Jul 23, 2016
d7aa40d
Remove unnecessary subtype reduction operations
ahejlsberg Jul 23, 2016
b673d5f
Use binary searching in union types to improve performance
ahejlsberg Jul 23, 2016
ff0cbb5
Merge branch 'master' into literalTypes
ahejlsberg Jul 24, 2016
a1a8725
Optimize type inference
ahejlsberg Jul 25, 2016
a5fcd5f
Display enum member types using qualified names
ahejlsberg Jul 28, 2016
5ff07dc
Accept new baselines
ahejlsberg Jul 28, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
22 changes: 4 additions & 18 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,18 +618,10 @@ namespace ts {
return false;
}

function isNarrowingNullCheckOperands(expr1: Expression, expr2: Expression) {
return (expr1.kind === SyntaxKind.NullKeyword || expr1.kind === SyntaxKind.Identifier && (<Identifier>expr1).text === "undefined") && isNarrowableOperand(expr2);
}

function isNarrowingTypeofOperands(expr1: Expression, expr2: Expression) {
return expr1.kind === SyntaxKind.TypeOfExpression && isNarrowableOperand((<TypeOfExpression>expr1).expression) && expr2.kind === SyntaxKind.StringLiteral;
}

function isNarrowingDiscriminant(expr: Expression) {
return expr.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((<PropertyAccessExpression>expr).expression);
}

function isNarrowingBinaryExpression(expr: BinaryExpression) {
switch (expr.operatorToken.kind) {
case SyntaxKind.EqualsToken:
Expand All @@ -638,9 +630,8 @@ namespace ts {
case SyntaxKind.ExclamationEqualsToken:
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsEqualsToken:
return isNarrowingNullCheckOperands(expr.right, expr.left) || isNarrowingNullCheckOperands(expr.left, expr.right) ||
isNarrowingTypeofOperands(expr.right, expr.left) || isNarrowingTypeofOperands(expr.left, expr.right) ||
isNarrowingDiscriminant(expr.left) || isNarrowingDiscriminant(expr.right);
return isNarrowableOperand(expr.left) || isNarrowableOperand(expr.right) ||
isNarrowingTypeofOperands(expr.right, expr.left) || isNarrowingTypeofOperands(expr.left, expr.right);
case SyntaxKind.InstanceOfKeyword:
return isNarrowableOperand(expr.left);
case SyntaxKind.CommaToken:
Expand All @@ -664,11 +655,6 @@ namespace ts {
return isNarrowableReference(expr);
}

function isNarrowingSwitchStatement(switchStatement: SwitchStatement) {
const expr = switchStatement.expression;
return expr.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((<PropertyAccessExpression>expr).expression);
}

function createBranchLabel(): FlowLabel {
return {
flags: FlowFlags.BranchLabel,
Expand Down Expand Up @@ -718,7 +704,7 @@ namespace ts {
}

function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode {
if (!isNarrowingSwitchStatement(switchStatement)) {
if (!isNarrowingExpression(switchStatement.expression)) {
return antecedent;
}
setFlowNodeReferenced(antecedent);
Expand Down Expand Up @@ -1983,7 +1969,7 @@ namespace ts {
function bindThisPropertyAssignment(node: BinaryExpression) {
// Declare a 'member' in case it turns out the container was an ES5 class or ES6 constructor
let assignee: Node;
if (container.kind === SyntaxKind.FunctionDeclaration || container.kind === SyntaxKind.FunctionDeclaration) {
if (container.kind === SyntaxKind.FunctionDeclaration || container.kind === SyntaxKind.FunctionExpression) {
assignee = container;
}
else if (container.kind === SyntaxKind.Constructor) {
Expand Down
1,111 changes: 734 additions & 377 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ namespace ts {
return undefined;
}

export function contains<T>(array: T[], value: T, areEqual?: (a: T, b: T) => boolean): boolean {
export function contains<T>(array: T[], value: T): boolean {
if (array) {
for (const v of array) {
if (areEqual ? areEqual(v, value) : v === value) {
if (v === value) {
return true;
}
}
Expand Down Expand Up @@ -182,10 +182,13 @@ namespace ts {
let result: T[];
if (array) {
result = [];
for (const item of array) {
if (!contains(result, item, areEqual)) {
result.push(item);
loop: for (const item of array) {
for (const res of result) {
if (areEqual ? areEqual(res, item) : res === item) {
continue loop;
}
}
result.push(item);
}
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ namespace ts {
case SyntaxKind.NullKeyword:
case SyntaxKind.NeverKeyword:
case SyntaxKind.ThisType:
case SyntaxKind.StringLiteralType:
case SyntaxKind.LiteralType:
return writeTextOfNode(currentText, type);
case SyntaxKind.ExpressionWithTypeArguments:
return emitExpressionWithTypeArguments(<ExpressionWithTypeArguments>type);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,10 @@
"category": "Error",
"code": 2534
},
"Enum type '{0}' has members with initializers that are not literals.": {
"category": "Error",
"code": 2535
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",
"code": 2600
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6041,7 +6041,7 @@ const _super = (function (geti, seti) {
return;

case SyntaxKind.StringKeyword:
case SyntaxKind.StringLiteralType:
case SyntaxKind.LiteralType:
write("String");
return;

Expand Down
Loading