From 03f464f433af05d77c0f3afb6c9674a3bcb3da2c Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 26 May 2018 08:51:09 -0700 Subject: [PATCH] Add 'unknown' keyword to scanner/parser/emitter --- src/compiler/parser.ts | 2 + src/compiler/scanner.ts | 1 + src/compiler/transformers/ts.ts | 2 + src/compiler/types.ts | 67 ++++++++++++++++++--------------- src/compiler/utilities.ts | 2 + 5 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 03a89343045..ea5a2a33937 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2850,6 +2850,7 @@ namespace ts { function parseNonArrayType(): TypeNode { switch (token()) { case SyntaxKind.AnyKeyword: + case SyntaxKind.UnknownKeyword: case SyntaxKind.StringKeyword: case SyntaxKind.NumberKeyword: case SyntaxKind.SymbolKeyword: @@ -2907,6 +2908,7 @@ namespace ts { function isStartOfType(inStartOfParameter?: boolean): boolean { switch (token()) { case SyntaxKind.AnyKeyword: + case SyntaxKind.UnknownKeyword: case SyntaxKind.StringKeyword: case SyntaxKind.NumberKeyword: case SyntaxKind.BooleanKeyword: diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 41b65c67864..af74d69ceef 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -124,6 +124,7 @@ namespace ts { "typeof": SyntaxKind.TypeOfKeyword, "undefined": SyntaxKind.UndefinedKeyword, "unique": SyntaxKind.UniqueKeyword, + "unknown": SyntaxKind.UnknownKeyword, "var": SyntaxKind.VarKeyword, "void": SyntaxKind.VoidKeyword, "while": SyntaxKind.WhileKeyword, diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 5ae6bcb8b32..cc244d92204 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -384,6 +384,7 @@ namespace ts { case SyntaxKind.TypePredicate: case SyntaxKind.TypeParameter: case SyntaxKind.AnyKeyword: + case SyntaxKind.UnknownKeyword: case SyntaxKind.BooleanKeyword: case SyntaxKind.StringKeyword: case SyntaxKind.NumberKeyword: @@ -1907,6 +1908,7 @@ namespace ts { case SyntaxKind.MappedType: case SyntaxKind.TypeLiteral: case SyntaxKind.AnyKeyword: + case SyntaxKind.UnknownKeyword: case SyntaxKind.ThisType: break; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index daccfd28282..b13f4aea613 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -241,6 +241,7 @@ namespace ts { TypeKeyword, UndefinedKeyword, UniqueKeyword, + UnknownKeyword, FromKeyword, GlobalKeyword, OfKeyword, // LastKeyword and LastToken and LastContextualKeyword @@ -1068,6 +1069,7 @@ namespace ts { export interface KeywordTypeNode extends TypeNode { kind: SyntaxKind.AnyKeyword + | SyntaxKind.UnknownKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.BooleanKeyword @@ -3665,40 +3667,43 @@ namespace ts { export const enum TypeFlags { Any = 1 << 0, - String = 1 << 1, - Number = 1 << 2, - Boolean = 1 << 3, - Enum = 1 << 4, - StringLiteral = 1 << 5, - NumberLiteral = 1 << 6, - BooleanLiteral = 1 << 7, - EnumLiteral = 1 << 8, // Always combined with StringLiteral, NumberLiteral, or Union - ESSymbol = 1 << 9, // Type of symbol primitive introduced in ES6 - UniqueESSymbol = 1 << 10, // unique symbol - Void = 1 << 11, - Undefined = 1 << 12, - Null = 1 << 13, - Never = 1 << 14, // Never type - TypeParameter = 1 << 15, // Type parameter - Object = 1 << 16, // Object type - Union = 1 << 17, // Union (T | U) - Intersection = 1 << 18, // Intersection (T & U) - Index = 1 << 19, // keyof T - IndexedAccess = 1 << 20, // T[K] - Conditional = 1 << 21, // T extends U ? X : Y - Substitution = 1 << 22, // Type parameter substitution - NonPrimitive = 1 << 23, // intrinsic object type + Unknown = 1 << 1, + String = 1 << 2, + Number = 1 << 3, + Boolean = 1 << 4, + Enum = 1 << 5, + StringLiteral = 1 << 6, + NumberLiteral = 1 << 7, + BooleanLiteral = 1 << 8, + EnumLiteral = 1 << 9, // Always combined with StringLiteral, NumberLiteral, or Union + ESSymbol = 1 << 10, // Type of symbol primitive introduced in ES6 + UniqueESSymbol = 1 << 11, // unique symbol + Void = 1 << 12, + Undefined = 1 << 13, + Null = 1 << 14, + Never = 1 << 15, // Never type + TypeParameter = 1 << 16, // Type parameter + Object = 1 << 17, // Object type + Union = 1 << 18, // Union (T | U) + Intersection = 1 << 19, // Intersection (T & U) + Index = 1 << 20, // keyof T + IndexedAccess = 1 << 21, // T[K] + Conditional = 1 << 22, // T extends U ? X : Y + Substitution = 1 << 23, // Type parameter substitution + NonPrimitive = 1 << 24, // intrinsic object type /* @internal */ - FreshLiteral = 1 << 24, // Fresh literal or unique type + FreshLiteral = 1 << 25, // Fresh literal or unique type /* @internal */ - UnionOfUnitTypes = 1 << 25, // Type is union of unit types + UnionOfUnitTypes = 1 << 26, // Type is union of unit types /* @internal */ - ContainsWideningType = 1 << 26, // Type is or contains undefined or null widening type + ContainsWideningType = 1 << 27, // Type is or contains undefined or null widening type /* @internal */ - ContainsObjectLiteral = 1 << 27, // Type is or contains object literal type + ContainsObjectLiteral = 1 << 28, // Type is or contains object literal type /* @internal */ - ContainsAnyFunctionType = 1 << 28, // Type is or contains the anyFunctionType + ContainsAnyFunctionType = 1 << 29, // Type is or contains the anyFunctionType + /* @internal */ + AnyOrUnknown = Any | Unknown, /* @internal */ Nullable = Undefined | Null, Literal = StringLiteral | NumberLiteral | BooleanLiteral, @@ -3710,7 +3715,7 @@ namespace ts { DefinitelyFalsy = StringLiteral | NumberLiteral | BooleanLiteral | Void | Undefined | Null, PossiblyFalsy = DefinitelyFalsy | String | Number | Boolean, /* @internal */ - Intrinsic = Any | String | Number | Boolean | BooleanLiteral | ESSymbol | Void | Undefined | Null | Never | NonPrimitive, + Intrinsic = Any | Unknown | String | Number | Boolean | BooleanLiteral | ESSymbol | Void | Undefined | Null | Never | NonPrimitive, /* @internal */ Primitive = String | Number | Boolean | Enum | EnumLiteral | ESSymbol | Void | Undefined | Null | Literal | UniqueESSymbol, StringLike = String | StringLiteral, @@ -3731,8 +3736,8 @@ namespace ts { // 'Narrowable' types are types where narrowing actually narrows. // This *should* be every type other than null, undefined, void, and never - Narrowable = Any | StructuredOrInstantiable | StringLike | NumberLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive, - NotUnionOrUnit = Any | ESSymbol | Object | NonPrimitive, + Narrowable = Any | Unknown | StructuredOrInstantiable | StringLike | NumberLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive, + NotUnionOrUnit = Any | Unknown | ESSymbol | Object | NonPrimitive, /* @internal */ NotUnit = Any | String | Number | Boolean | Enum | ESSymbol | Void | Never | StructuredOrInstantiable, /* @internal */ diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index a3da44c5b1f..2cc3c645ebe 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -810,6 +810,7 @@ namespace ts { switch (node.kind) { case SyntaxKind.AnyKeyword: + case SyntaxKind.UnknownKeyword: case SyntaxKind.NumberKeyword: case SyntaxKind.StringKeyword: case SyntaxKind.BooleanKeyword: @@ -5777,6 +5778,7 @@ namespace ts { function isTypeNodeKind(kind: SyntaxKind) { return (kind >= SyntaxKind.FirstTypeNode && kind <= SyntaxKind.LastTypeNode) || kind === SyntaxKind.AnyKeyword + || kind === SyntaxKind.UnknownKeyword || kind === SyntaxKind.NumberKeyword || kind === SyntaxKind.ObjectKeyword || kind === SyntaxKind.BooleanKeyword