Skip to content

Commit a8a31ed

Browse files
authored
Merge pull request microsoft#24439 from Microsoft/unknownType
New 'unknown' top type
2 parents 675e212 + 8664390 commit a8a31ed

File tree

14 files changed

+2248
-635
lines changed

14 files changed

+2248
-635
lines changed

src/compiler/checker.ts

Lines changed: 192 additions & 171 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,6 +2024,10 @@
20242024
"category": "Error",
20252025
"code": 2570
20262026
},
2027+
"Object is of type 'unknown'.": {
2028+
"category": "Error",
2029+
"code": 2571
2030+
},
20272031
"JSX element attributes type '{0}' may not be a union type.": {
20282032
"category": "Error",
20292033
"code": 2600

src/compiler/parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,7 @@ namespace ts {
28502850
function parseNonArrayType(): TypeNode {
28512851
switch (token()) {
28522852
case SyntaxKind.AnyKeyword:
2853+
case SyntaxKind.UnknownKeyword:
28532854
case SyntaxKind.StringKeyword:
28542855
case SyntaxKind.NumberKeyword:
28552856
case SyntaxKind.SymbolKeyword:
@@ -2907,6 +2908,7 @@ namespace ts {
29072908
function isStartOfType(inStartOfParameter?: boolean): boolean {
29082909
switch (token()) {
29092910
case SyntaxKind.AnyKeyword:
2911+
case SyntaxKind.UnknownKeyword:
29102912
case SyntaxKind.StringKeyword:
29112913
case SyntaxKind.NumberKeyword:
29122914
case SyntaxKind.BooleanKeyword:

src/compiler/scanner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ namespace ts {
124124
"typeof": SyntaxKind.TypeOfKeyword,
125125
"undefined": SyntaxKind.UndefinedKeyword,
126126
"unique": SyntaxKind.UniqueKeyword,
127+
"unknown": SyntaxKind.UnknownKeyword,
127128
"var": SyntaxKind.VarKeyword,
128129
"void": SyntaxKind.VoidKeyword,
129130
"while": SyntaxKind.WhileKeyword,

src/compiler/transformers/ts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ namespace ts {
384384
case SyntaxKind.TypePredicate:
385385
case SyntaxKind.TypeParameter:
386386
case SyntaxKind.AnyKeyword:
387+
case SyntaxKind.UnknownKeyword:
387388
case SyntaxKind.BooleanKeyword:
388389
case SyntaxKind.StringKeyword:
389390
case SyntaxKind.NumberKeyword:
@@ -1907,6 +1908,7 @@ namespace ts {
19071908
case SyntaxKind.MappedType:
19081909
case SyntaxKind.TypeLiteral:
19091910
case SyntaxKind.AnyKeyword:
1911+
case SyntaxKind.UnknownKeyword:
19101912
case SyntaxKind.ThisType:
19111913
break;
19121914

src/compiler/types.ts

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ namespace ts {
241241
TypeKeyword,
242242
UndefinedKeyword,
243243
UniqueKeyword,
244+
UnknownKeyword,
244245
FromKeyword,
245246
GlobalKeyword,
246247
OfKeyword, // LastKeyword and LastToken and LastContextualKeyword
@@ -1068,6 +1069,7 @@ namespace ts {
10681069

10691070
export interface KeywordTypeNode extends TypeNode {
10701071
kind: SyntaxKind.AnyKeyword
1072+
| SyntaxKind.UnknownKeyword
10711073
| SyntaxKind.NumberKeyword
10721074
| SyntaxKind.ObjectKeyword
10731075
| SyntaxKind.BooleanKeyword
@@ -3665,42 +3667,43 @@ namespace ts {
36653667

36663668
export const enum TypeFlags {
36673669
Any = 1 << 0,
3668-
String = 1 << 1,
3669-
Number = 1 << 2,
3670-
Boolean = 1 << 3,
3671-
Enum = 1 << 4,
3672-
StringLiteral = 1 << 5,
3673-
NumberLiteral = 1 << 6,
3674-
BooleanLiteral = 1 << 7,
3675-
EnumLiteral = 1 << 8, // Always combined with StringLiteral, NumberLiteral, or Union
3676-
ESSymbol = 1 << 9, // Type of symbol primitive introduced in ES6
3677-
UniqueESSymbol = 1 << 10, // unique symbol
3678-
Void = 1 << 11,
3679-
Undefined = 1 << 12,
3680-
Null = 1 << 13,
3681-
Never = 1 << 14, // Never type
3682-
TypeParameter = 1 << 15, // Type parameter
3683-
Object = 1 << 16, // Object type
3684-
Union = 1 << 17, // Union (T | U)
3685-
Intersection = 1 << 18, // Intersection (T & U)
3686-
Index = 1 << 19, // keyof T
3687-
IndexedAccess = 1 << 20, // T[K]
3688-
Conditional = 1 << 21, // T extends U ? X : Y
3689-
Substitution = 1 << 22, // Type parameter substitution
3670+
Unknown = 1 << 1,
3671+
String = 1 << 2,
3672+
Number = 1 << 3,
3673+
Boolean = 1 << 4,
3674+
Enum = 1 << 5,
3675+
StringLiteral = 1 << 6,
3676+
NumberLiteral = 1 << 7,
3677+
BooleanLiteral = 1 << 8,
3678+
EnumLiteral = 1 << 9, // Always combined with StringLiteral, NumberLiteral, or Union
3679+
ESSymbol = 1 << 10, // Type of symbol primitive introduced in ES6
3680+
UniqueESSymbol = 1 << 11, // unique symbol
3681+
Void = 1 << 12,
3682+
Undefined = 1 << 13,
3683+
Null = 1 << 14,
3684+
Never = 1 << 15, // Never type
3685+
TypeParameter = 1 << 16, // Type parameter
3686+
Object = 1 << 17, // Object type
3687+
Union = 1 << 18, // Union (T | U)
3688+
Intersection = 1 << 19, // Intersection (T & U)
3689+
Index = 1 << 20, // keyof T
3690+
IndexedAccess = 1 << 21, // T[K]
3691+
Conditional = 1 << 22, // T extends U ? X : Y
3692+
Substitution = 1 << 23, // Type parameter substitution
3693+
NonPrimitive = 1 << 24, // intrinsic object type
36903694
/* @internal */
3691-
FreshLiteral = 1 << 23, // Fresh literal or unique type
3695+
FreshLiteral = 1 << 25, // Fresh literal or unique type
36923696
/* @internal */
3693-
ContainsWideningType = 1 << 24, // Type is or contains undefined or null widening type
3697+
UnionOfUnitTypes = 1 << 26, // Type is union of unit types
36943698
/* @internal */
3695-
ContainsObjectLiteral = 1 << 25, // Type is or contains object literal type
3699+
ContainsWideningType = 1 << 27, // Type is or contains undefined or null widening type
36963700
/* @internal */
3697-
ContainsAnyFunctionType = 1 << 26, // Type is or contains the anyFunctionType
3698-
NonPrimitive = 1 << 27, // intrinsic object type
3701+
ContainsObjectLiteral = 1 << 28, // Type is or contains object literal type
36993702
/* @internal */
3700-
UnionOfUnitTypes = 1 << 28, // Type is union of unit types
3701-
/* @internal */
3702-
GenericMappedType = 1 << 29, // Flag used by maybeTypeOfKind
3703+
ContainsAnyFunctionType = 1 << 29, // Type is or contains the anyFunctionType
37033704

3705+
/* @internal */
3706+
AnyOrUnknown = Any | Unknown,
37043707
/* @internal */
37053708
Nullable = Undefined | Null,
37063709
Literal = StringLiteral | NumberLiteral | BooleanLiteral,
@@ -3712,7 +3715,7 @@ namespace ts {
37123715
DefinitelyFalsy = StringLiteral | NumberLiteral | BooleanLiteral | Void | Undefined | Null,
37133716
PossiblyFalsy = DefinitelyFalsy | String | Number | Boolean,
37143717
/* @internal */
3715-
Intrinsic = Any | String | Number | Boolean | BooleanLiteral | ESSymbol | Void | Undefined | Null | Never | NonPrimitive,
3718+
Intrinsic = Any | Unknown | String | Number | Boolean | BooleanLiteral | ESSymbol | Void | Undefined | Null | Never | NonPrimitive,
37163719
/* @internal */
37173720
Primitive = String | Number | Boolean | Enum | EnumLiteral | ESSymbol | Void | Undefined | Null | Literal | UniqueESSymbol,
37183721
StringLike = String | StringLiteral,
@@ -3733,8 +3736,8 @@ namespace ts {
37333736

37343737
// 'Narrowable' types are types where narrowing actually narrows.
37353738
// This *should* be every type other than null, undefined, void, and never
3736-
Narrowable = Any | StructuredOrInstantiable | StringLike | NumberLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,
3737-
NotUnionOrUnit = Any | ESSymbol | Object | NonPrimitive,
3739+
Narrowable = Any | Unknown | StructuredOrInstantiable | StringLike | NumberLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,
3740+
NotUnionOrUnit = Any | Unknown | ESSymbol | Object | NonPrimitive,
37383741
/* @internal */
37393742
NotUnit = Any | String | Number | Boolean | Enum | ESSymbol | Void | Never | StructuredOrInstantiable,
37403743
/* @internal */
@@ -3749,7 +3752,10 @@ namespace ts {
37493752
/* @internal */
37503753
EmptyObject = ContainsAnyFunctionType,
37513754
/* @internal */
3752-
ConstructionFlags = NonWideningType | Wildcard | EmptyObject
3755+
ConstructionFlags = NonWideningType | Wildcard | EmptyObject,
3756+
// The following flag is used for different purposes by maybeTypeOfKind
3757+
/* @internal */
3758+
GenericMappedType = ContainsWideningType
37533759
}
37543760

37553761
export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;

src/compiler/utilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ namespace ts {
810810

811811
switch (node.kind) {
812812
case SyntaxKind.AnyKeyword:
813+
case SyntaxKind.UnknownKeyword:
813814
case SyntaxKind.NumberKeyword:
814815
case SyntaxKind.StringKeyword:
815816
case SyntaxKind.BooleanKeyword:
@@ -5782,6 +5783,7 @@ namespace ts {
57825783
function isTypeNodeKind(kind: SyntaxKind) {
57835784
return (kind >= SyntaxKind.FirstTypeNode && kind <= SyntaxKind.LastTypeNode)
57845785
|| kind === SyntaxKind.AnyKeyword
5786+
|| kind === SyntaxKind.UnknownKeyword
57855787
|| kind === SyntaxKind.NumberKeyword
57865788
|| kind === SyntaxKind.ObjectKeyword
57875789
|| kind === SyntaxKind.BooleanKeyword

0 commit comments

Comments
 (0)