Merge pull request #24439 from Microsoft/unknownType

New 'unknown' top type
This commit is contained in:
Anders Hejlsberg 2018-05-30 10:22:02 -07:00 committed by GitHub
commit a8a31ed508
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 2248 additions and 635 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2024,6 +2024,10 @@
"category": "Error",
"code": 2570
},
"Object is of type 'unknown'.": {
"category": "Error",
"code": 2571
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",
"code": 2600

View File

@ -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:

View File

@ -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,

View File

@ -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;

View File

@ -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,42 +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
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 << 23, // Fresh literal or unique type
FreshLiteral = 1 << 25, // Fresh literal or unique type
/* @internal */
ContainsWideningType = 1 << 24, // Type is or contains undefined or null widening type
UnionOfUnitTypes = 1 << 26, // Type is union of unit types
/* @internal */
ContainsObjectLiteral = 1 << 25, // Type is or contains object literal type
ContainsWideningType = 1 << 27, // Type is or contains undefined or null widening type
/* @internal */
ContainsAnyFunctionType = 1 << 26, // Type is or contains the anyFunctionType
NonPrimitive = 1 << 27, // intrinsic object type
ContainsObjectLiteral = 1 << 28, // Type is or contains object literal type
/* @internal */
UnionOfUnitTypes = 1 << 28, // Type is union of unit types
/* @internal */
GenericMappedType = 1 << 29, // Flag used by maybeTypeOfKind
ContainsAnyFunctionType = 1 << 29, // Type is or contains the anyFunctionType
/* @internal */
AnyOrUnknown = Any | Unknown,
/* @internal */
Nullable = Undefined | Null,
Literal = StringLiteral | NumberLiteral | BooleanLiteral,
@ -3712,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,
@ -3733,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 */
@ -3749,7 +3752,10 @@ namespace ts {
/* @internal */
EmptyObject = ContainsAnyFunctionType,
/* @internal */
ConstructionFlags = NonWideningType | Wildcard | EmptyObject
ConstructionFlags = NonWideningType | Wildcard | EmptyObject,
// The following flag is used for different purposes by maybeTypeOfKind
/* @internal */
GenericMappedType = ContainsWideningType
}
export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;

View File

@ -810,6 +810,7 @@ namespace ts {
switch (node.kind) {
case SyntaxKind.AnyKeyword:
case SyntaxKind.UnknownKeyword:
case SyntaxKind.NumberKeyword:
case SyntaxKind.StringKeyword:
case SyntaxKind.BooleanKeyword:
@ -5782,6 +5783,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

View File

@ -204,169 +204,170 @@ declare namespace ts {
TypeKeyword = 139,
UndefinedKeyword = 140,
UniqueKeyword = 141,
FromKeyword = 142,
GlobalKeyword = 143,
OfKeyword = 144,
QualifiedName = 145,
ComputedPropertyName = 146,
TypeParameter = 147,
Parameter = 148,
Decorator = 149,
PropertySignature = 150,
PropertyDeclaration = 151,
MethodSignature = 152,
MethodDeclaration = 153,
Constructor = 154,
GetAccessor = 155,
SetAccessor = 156,
CallSignature = 157,
ConstructSignature = 158,
IndexSignature = 159,
TypePredicate = 160,
TypeReference = 161,
FunctionType = 162,
ConstructorType = 163,
TypeQuery = 164,
TypeLiteral = 165,
ArrayType = 166,
TupleType = 167,
UnionType = 168,
IntersectionType = 169,
ConditionalType = 170,
InferType = 171,
ParenthesizedType = 172,
ThisType = 173,
TypeOperator = 174,
IndexedAccessType = 175,
MappedType = 176,
LiteralType = 177,
ImportType = 178,
ObjectBindingPattern = 179,
ArrayBindingPattern = 180,
BindingElement = 181,
ArrayLiteralExpression = 182,
ObjectLiteralExpression = 183,
PropertyAccessExpression = 184,
ElementAccessExpression = 185,
CallExpression = 186,
NewExpression = 187,
TaggedTemplateExpression = 188,
TypeAssertionExpression = 189,
ParenthesizedExpression = 190,
FunctionExpression = 191,
ArrowFunction = 192,
DeleteExpression = 193,
TypeOfExpression = 194,
VoidExpression = 195,
AwaitExpression = 196,
PrefixUnaryExpression = 197,
PostfixUnaryExpression = 198,
BinaryExpression = 199,
ConditionalExpression = 200,
TemplateExpression = 201,
YieldExpression = 202,
SpreadElement = 203,
ClassExpression = 204,
OmittedExpression = 205,
ExpressionWithTypeArguments = 206,
AsExpression = 207,
NonNullExpression = 208,
MetaProperty = 209,
TemplateSpan = 210,
SemicolonClassElement = 211,
Block = 212,
VariableStatement = 213,
EmptyStatement = 214,
ExpressionStatement = 215,
IfStatement = 216,
DoStatement = 217,
WhileStatement = 218,
ForStatement = 219,
ForInStatement = 220,
ForOfStatement = 221,
ContinueStatement = 222,
BreakStatement = 223,
ReturnStatement = 224,
WithStatement = 225,
SwitchStatement = 226,
LabeledStatement = 227,
ThrowStatement = 228,
TryStatement = 229,
DebuggerStatement = 230,
VariableDeclaration = 231,
VariableDeclarationList = 232,
FunctionDeclaration = 233,
ClassDeclaration = 234,
InterfaceDeclaration = 235,
TypeAliasDeclaration = 236,
EnumDeclaration = 237,
ModuleDeclaration = 238,
ModuleBlock = 239,
CaseBlock = 240,
NamespaceExportDeclaration = 241,
ImportEqualsDeclaration = 242,
ImportDeclaration = 243,
ImportClause = 244,
NamespaceImport = 245,
NamedImports = 246,
ImportSpecifier = 247,
ExportAssignment = 248,
ExportDeclaration = 249,
NamedExports = 250,
ExportSpecifier = 251,
MissingDeclaration = 252,
ExternalModuleReference = 253,
JsxElement = 254,
JsxSelfClosingElement = 255,
JsxOpeningElement = 256,
JsxClosingElement = 257,
JsxFragment = 258,
JsxOpeningFragment = 259,
JsxClosingFragment = 260,
JsxAttribute = 261,
JsxAttributes = 262,
JsxSpreadAttribute = 263,
JsxExpression = 264,
CaseClause = 265,
DefaultClause = 266,
HeritageClause = 267,
CatchClause = 268,
PropertyAssignment = 269,
ShorthandPropertyAssignment = 270,
SpreadAssignment = 271,
EnumMember = 272,
SourceFile = 273,
Bundle = 274,
UnparsedSource = 275,
InputFiles = 276,
JSDocTypeExpression = 277,
JSDocAllType = 278,
JSDocUnknownType = 279,
JSDocNullableType = 280,
JSDocNonNullableType = 281,
JSDocOptionalType = 282,
JSDocFunctionType = 283,
JSDocVariadicType = 284,
JSDocComment = 285,
JSDocTypeLiteral = 286,
JSDocSignature = 287,
JSDocTag = 288,
JSDocAugmentsTag = 289,
JSDocClassTag = 290,
JSDocCallbackTag = 291,
JSDocParameterTag = 292,
JSDocReturnTag = 293,
JSDocTypeTag = 294,
JSDocTemplateTag = 295,
JSDocTypedefTag = 296,
JSDocPropertyTag = 297,
SyntaxList = 298,
NotEmittedStatement = 299,
PartiallyEmittedExpression = 300,
CommaListExpression = 301,
MergeDeclarationMarker = 302,
EndOfDeclarationMarker = 303,
Count = 304,
UnknownKeyword = 142,
FromKeyword = 143,
GlobalKeyword = 144,
OfKeyword = 145,
QualifiedName = 146,
ComputedPropertyName = 147,
TypeParameter = 148,
Parameter = 149,
Decorator = 150,
PropertySignature = 151,
PropertyDeclaration = 152,
MethodSignature = 153,
MethodDeclaration = 154,
Constructor = 155,
GetAccessor = 156,
SetAccessor = 157,
CallSignature = 158,
ConstructSignature = 159,
IndexSignature = 160,
TypePredicate = 161,
TypeReference = 162,
FunctionType = 163,
ConstructorType = 164,
TypeQuery = 165,
TypeLiteral = 166,
ArrayType = 167,
TupleType = 168,
UnionType = 169,
IntersectionType = 170,
ConditionalType = 171,
InferType = 172,
ParenthesizedType = 173,
ThisType = 174,
TypeOperator = 175,
IndexedAccessType = 176,
MappedType = 177,
LiteralType = 178,
ImportType = 179,
ObjectBindingPattern = 180,
ArrayBindingPattern = 181,
BindingElement = 182,
ArrayLiteralExpression = 183,
ObjectLiteralExpression = 184,
PropertyAccessExpression = 185,
ElementAccessExpression = 186,
CallExpression = 187,
NewExpression = 188,
TaggedTemplateExpression = 189,
TypeAssertionExpression = 190,
ParenthesizedExpression = 191,
FunctionExpression = 192,
ArrowFunction = 193,
DeleteExpression = 194,
TypeOfExpression = 195,
VoidExpression = 196,
AwaitExpression = 197,
PrefixUnaryExpression = 198,
PostfixUnaryExpression = 199,
BinaryExpression = 200,
ConditionalExpression = 201,
TemplateExpression = 202,
YieldExpression = 203,
SpreadElement = 204,
ClassExpression = 205,
OmittedExpression = 206,
ExpressionWithTypeArguments = 207,
AsExpression = 208,
NonNullExpression = 209,
MetaProperty = 210,
TemplateSpan = 211,
SemicolonClassElement = 212,
Block = 213,
VariableStatement = 214,
EmptyStatement = 215,
ExpressionStatement = 216,
IfStatement = 217,
DoStatement = 218,
WhileStatement = 219,
ForStatement = 220,
ForInStatement = 221,
ForOfStatement = 222,
ContinueStatement = 223,
BreakStatement = 224,
ReturnStatement = 225,
WithStatement = 226,
SwitchStatement = 227,
LabeledStatement = 228,
ThrowStatement = 229,
TryStatement = 230,
DebuggerStatement = 231,
VariableDeclaration = 232,
VariableDeclarationList = 233,
FunctionDeclaration = 234,
ClassDeclaration = 235,
InterfaceDeclaration = 236,
TypeAliasDeclaration = 237,
EnumDeclaration = 238,
ModuleDeclaration = 239,
ModuleBlock = 240,
CaseBlock = 241,
NamespaceExportDeclaration = 242,
ImportEqualsDeclaration = 243,
ImportDeclaration = 244,
ImportClause = 245,
NamespaceImport = 246,
NamedImports = 247,
ImportSpecifier = 248,
ExportAssignment = 249,
ExportDeclaration = 250,
NamedExports = 251,
ExportSpecifier = 252,
MissingDeclaration = 253,
ExternalModuleReference = 254,
JsxElement = 255,
JsxSelfClosingElement = 256,
JsxOpeningElement = 257,
JsxClosingElement = 258,
JsxFragment = 259,
JsxOpeningFragment = 260,
JsxClosingFragment = 261,
JsxAttribute = 262,
JsxAttributes = 263,
JsxSpreadAttribute = 264,
JsxExpression = 265,
CaseClause = 266,
DefaultClause = 267,
HeritageClause = 268,
CatchClause = 269,
PropertyAssignment = 270,
ShorthandPropertyAssignment = 271,
SpreadAssignment = 272,
EnumMember = 273,
SourceFile = 274,
Bundle = 275,
UnparsedSource = 276,
InputFiles = 277,
JSDocTypeExpression = 278,
JSDocAllType = 279,
JSDocUnknownType = 280,
JSDocNullableType = 281,
JSDocNonNullableType = 282,
JSDocOptionalType = 283,
JSDocFunctionType = 284,
JSDocVariadicType = 285,
JSDocComment = 286,
JSDocTypeLiteral = 287,
JSDocSignature = 288,
JSDocTag = 289,
JSDocAugmentsTag = 290,
JSDocClassTag = 291,
JSDocCallbackTag = 292,
JSDocParameterTag = 293,
JSDocReturnTag = 294,
JSDocTypeTag = 295,
JSDocTemplateTag = 296,
JSDocTypedefTag = 297,
JSDocPropertyTag = 298,
SyntaxList = 299,
NotEmittedStatement = 300,
PartiallyEmittedExpression = 301,
CommaListExpression = 302,
MergeDeclarationMarker = 303,
EndOfDeclarationMarker = 304,
Count = 305,
FirstAssignment = 58,
LastAssignment = 70,
FirstCompoundAssignment = 59,
@ -374,15 +375,15 @@ declare namespace ts {
FirstReservedWord = 72,
LastReservedWord = 107,
FirstKeyword = 72,
LastKeyword = 144,
LastKeyword = 145,
FirstFutureReservedWord = 108,
LastFutureReservedWord = 116,
FirstTypeNode = 160,
LastTypeNode = 178,
FirstTypeNode = 161,
LastTypeNode = 179,
FirstPunctuation = 17,
LastPunctuation = 70,
FirstToken = 0,
LastToken = 144,
LastToken = 145,
FirstTriviaToken = 2,
LastTriviaToken = 7,
FirstLiteralToken = 8,
@ -391,11 +392,11 @@ declare namespace ts {
LastTemplateToken = 16,
FirstBinaryOperator = 27,
LastBinaryOperator = 70,
FirstNode = 145,
FirstJSDocNode = 277,
LastJSDocNode = 297,
FirstJSDocTagNode = 288,
LastJSDocTagNode = 297
FirstNode = 146,
FirstJSDocNode = 278,
LastJSDocNode = 298,
FirstJSDocTagNode = 289,
LastJSDocTagNode = 298
}
enum NodeFlags {
None = 0,
@ -702,7 +703,7 @@ declare namespace ts {
_typeNodeBrand: any;
}
interface KeywordTypeNode extends TypeNode {
kind: SyntaxKind.AnyKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.StringKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.ThisKeyword | SyntaxKind.VoidKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.NullKeyword | SyntaxKind.NeverKeyword;
kind: SyntaxKind.AnyKeyword | SyntaxKind.UnknownKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.StringKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.ThisKeyword | SyntaxKind.VoidKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.NullKeyword | SyntaxKind.NeverKeyword;
}
interface ImportTypeNode extends NodeWithTypeArguments {
kind: SyntaxKind.ImportType;
@ -2128,48 +2129,49 @@ declare namespace ts {
type SymbolTable = UnderscoreEscapedMap<Symbol>;
enum TypeFlags {
Any = 1,
String = 2,
Number = 4,
Boolean = 8,
Enum = 16,
StringLiteral = 32,
NumberLiteral = 64,
BooleanLiteral = 128,
EnumLiteral = 256,
ESSymbol = 512,
UniqueESSymbol = 1024,
Void = 2048,
Undefined = 4096,
Null = 8192,
Never = 16384,
TypeParameter = 32768,
Object = 65536,
Union = 131072,
Intersection = 262144,
Index = 524288,
IndexedAccess = 1048576,
Conditional = 2097152,
Substitution = 4194304,
NonPrimitive = 134217728,
Literal = 224,
Unit = 13536,
StringOrNumberLiteral = 96,
PossiblyFalsy = 14574,
StringLike = 34,
NumberLike = 84,
BooleanLike = 136,
EnumLike = 272,
ESSymbolLike = 1536,
VoidLike = 6144,
UnionOrIntersection = 393216,
StructuredType = 458752,
TypeVariable = 1081344,
InstantiableNonPrimitive = 7372800,
InstantiablePrimitive = 524288,
Instantiable = 7897088,
StructuredOrInstantiable = 8355840,
Narrowable = 142575359,
NotUnionOrUnit = 134283777
Unknown = 2,
String = 4,
Number = 8,
Boolean = 16,
Enum = 32,
StringLiteral = 64,
NumberLiteral = 128,
BooleanLiteral = 256,
EnumLiteral = 512,
ESSymbol = 1024,
UniqueESSymbol = 2048,
Void = 4096,
Undefined = 8192,
Null = 16384,
Never = 32768,
TypeParameter = 65536,
Object = 131072,
Union = 262144,
Intersection = 524288,
Index = 1048576,
IndexedAccess = 2097152,
Conditional = 4194304,
Substitution = 8388608,
NonPrimitive = 16777216,
Literal = 448,
Unit = 27072,
StringOrNumberLiteral = 192,
PossiblyFalsy = 29148,
StringLike = 68,
NumberLike = 168,
BooleanLike = 272,
EnumLike = 544,
ESSymbolLike = 3072,
VoidLike = 12288,
UnionOrIntersection = 786432,
StructuredType = 917504,
TypeVariable = 2162688,
InstantiableNonPrimitive = 14745600,
InstantiablePrimitive = 1048576,
Instantiable = 15794176,
StructuredOrInstantiable = 16711680,
Narrowable = 33492479,
NotUnionOrUnit = 16909315
}
type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;
interface Type {

View File

@ -204,169 +204,170 @@ declare namespace ts {
TypeKeyword = 139,
UndefinedKeyword = 140,
UniqueKeyword = 141,
FromKeyword = 142,
GlobalKeyword = 143,
OfKeyword = 144,
QualifiedName = 145,
ComputedPropertyName = 146,
TypeParameter = 147,
Parameter = 148,
Decorator = 149,
PropertySignature = 150,
PropertyDeclaration = 151,
MethodSignature = 152,
MethodDeclaration = 153,
Constructor = 154,
GetAccessor = 155,
SetAccessor = 156,
CallSignature = 157,
ConstructSignature = 158,
IndexSignature = 159,
TypePredicate = 160,
TypeReference = 161,
FunctionType = 162,
ConstructorType = 163,
TypeQuery = 164,
TypeLiteral = 165,
ArrayType = 166,
TupleType = 167,
UnionType = 168,
IntersectionType = 169,
ConditionalType = 170,
InferType = 171,
ParenthesizedType = 172,
ThisType = 173,
TypeOperator = 174,
IndexedAccessType = 175,
MappedType = 176,
LiteralType = 177,
ImportType = 178,
ObjectBindingPattern = 179,
ArrayBindingPattern = 180,
BindingElement = 181,
ArrayLiteralExpression = 182,
ObjectLiteralExpression = 183,
PropertyAccessExpression = 184,
ElementAccessExpression = 185,
CallExpression = 186,
NewExpression = 187,
TaggedTemplateExpression = 188,
TypeAssertionExpression = 189,
ParenthesizedExpression = 190,
FunctionExpression = 191,
ArrowFunction = 192,
DeleteExpression = 193,
TypeOfExpression = 194,
VoidExpression = 195,
AwaitExpression = 196,
PrefixUnaryExpression = 197,
PostfixUnaryExpression = 198,
BinaryExpression = 199,
ConditionalExpression = 200,
TemplateExpression = 201,
YieldExpression = 202,
SpreadElement = 203,
ClassExpression = 204,
OmittedExpression = 205,
ExpressionWithTypeArguments = 206,
AsExpression = 207,
NonNullExpression = 208,
MetaProperty = 209,
TemplateSpan = 210,
SemicolonClassElement = 211,
Block = 212,
VariableStatement = 213,
EmptyStatement = 214,
ExpressionStatement = 215,
IfStatement = 216,
DoStatement = 217,
WhileStatement = 218,
ForStatement = 219,
ForInStatement = 220,
ForOfStatement = 221,
ContinueStatement = 222,
BreakStatement = 223,
ReturnStatement = 224,
WithStatement = 225,
SwitchStatement = 226,
LabeledStatement = 227,
ThrowStatement = 228,
TryStatement = 229,
DebuggerStatement = 230,
VariableDeclaration = 231,
VariableDeclarationList = 232,
FunctionDeclaration = 233,
ClassDeclaration = 234,
InterfaceDeclaration = 235,
TypeAliasDeclaration = 236,
EnumDeclaration = 237,
ModuleDeclaration = 238,
ModuleBlock = 239,
CaseBlock = 240,
NamespaceExportDeclaration = 241,
ImportEqualsDeclaration = 242,
ImportDeclaration = 243,
ImportClause = 244,
NamespaceImport = 245,
NamedImports = 246,
ImportSpecifier = 247,
ExportAssignment = 248,
ExportDeclaration = 249,
NamedExports = 250,
ExportSpecifier = 251,
MissingDeclaration = 252,
ExternalModuleReference = 253,
JsxElement = 254,
JsxSelfClosingElement = 255,
JsxOpeningElement = 256,
JsxClosingElement = 257,
JsxFragment = 258,
JsxOpeningFragment = 259,
JsxClosingFragment = 260,
JsxAttribute = 261,
JsxAttributes = 262,
JsxSpreadAttribute = 263,
JsxExpression = 264,
CaseClause = 265,
DefaultClause = 266,
HeritageClause = 267,
CatchClause = 268,
PropertyAssignment = 269,
ShorthandPropertyAssignment = 270,
SpreadAssignment = 271,
EnumMember = 272,
SourceFile = 273,
Bundle = 274,
UnparsedSource = 275,
InputFiles = 276,
JSDocTypeExpression = 277,
JSDocAllType = 278,
JSDocUnknownType = 279,
JSDocNullableType = 280,
JSDocNonNullableType = 281,
JSDocOptionalType = 282,
JSDocFunctionType = 283,
JSDocVariadicType = 284,
JSDocComment = 285,
JSDocTypeLiteral = 286,
JSDocSignature = 287,
JSDocTag = 288,
JSDocAugmentsTag = 289,
JSDocClassTag = 290,
JSDocCallbackTag = 291,
JSDocParameterTag = 292,
JSDocReturnTag = 293,
JSDocTypeTag = 294,
JSDocTemplateTag = 295,
JSDocTypedefTag = 296,
JSDocPropertyTag = 297,
SyntaxList = 298,
NotEmittedStatement = 299,
PartiallyEmittedExpression = 300,
CommaListExpression = 301,
MergeDeclarationMarker = 302,
EndOfDeclarationMarker = 303,
Count = 304,
UnknownKeyword = 142,
FromKeyword = 143,
GlobalKeyword = 144,
OfKeyword = 145,
QualifiedName = 146,
ComputedPropertyName = 147,
TypeParameter = 148,
Parameter = 149,
Decorator = 150,
PropertySignature = 151,
PropertyDeclaration = 152,
MethodSignature = 153,
MethodDeclaration = 154,
Constructor = 155,
GetAccessor = 156,
SetAccessor = 157,
CallSignature = 158,
ConstructSignature = 159,
IndexSignature = 160,
TypePredicate = 161,
TypeReference = 162,
FunctionType = 163,
ConstructorType = 164,
TypeQuery = 165,
TypeLiteral = 166,
ArrayType = 167,
TupleType = 168,
UnionType = 169,
IntersectionType = 170,
ConditionalType = 171,
InferType = 172,
ParenthesizedType = 173,
ThisType = 174,
TypeOperator = 175,
IndexedAccessType = 176,
MappedType = 177,
LiteralType = 178,
ImportType = 179,
ObjectBindingPattern = 180,
ArrayBindingPattern = 181,
BindingElement = 182,
ArrayLiteralExpression = 183,
ObjectLiteralExpression = 184,
PropertyAccessExpression = 185,
ElementAccessExpression = 186,
CallExpression = 187,
NewExpression = 188,
TaggedTemplateExpression = 189,
TypeAssertionExpression = 190,
ParenthesizedExpression = 191,
FunctionExpression = 192,
ArrowFunction = 193,
DeleteExpression = 194,
TypeOfExpression = 195,
VoidExpression = 196,
AwaitExpression = 197,
PrefixUnaryExpression = 198,
PostfixUnaryExpression = 199,
BinaryExpression = 200,
ConditionalExpression = 201,
TemplateExpression = 202,
YieldExpression = 203,
SpreadElement = 204,
ClassExpression = 205,
OmittedExpression = 206,
ExpressionWithTypeArguments = 207,
AsExpression = 208,
NonNullExpression = 209,
MetaProperty = 210,
TemplateSpan = 211,
SemicolonClassElement = 212,
Block = 213,
VariableStatement = 214,
EmptyStatement = 215,
ExpressionStatement = 216,
IfStatement = 217,
DoStatement = 218,
WhileStatement = 219,
ForStatement = 220,
ForInStatement = 221,
ForOfStatement = 222,
ContinueStatement = 223,
BreakStatement = 224,
ReturnStatement = 225,
WithStatement = 226,
SwitchStatement = 227,
LabeledStatement = 228,
ThrowStatement = 229,
TryStatement = 230,
DebuggerStatement = 231,
VariableDeclaration = 232,
VariableDeclarationList = 233,
FunctionDeclaration = 234,
ClassDeclaration = 235,
InterfaceDeclaration = 236,
TypeAliasDeclaration = 237,
EnumDeclaration = 238,
ModuleDeclaration = 239,
ModuleBlock = 240,
CaseBlock = 241,
NamespaceExportDeclaration = 242,
ImportEqualsDeclaration = 243,
ImportDeclaration = 244,
ImportClause = 245,
NamespaceImport = 246,
NamedImports = 247,
ImportSpecifier = 248,
ExportAssignment = 249,
ExportDeclaration = 250,
NamedExports = 251,
ExportSpecifier = 252,
MissingDeclaration = 253,
ExternalModuleReference = 254,
JsxElement = 255,
JsxSelfClosingElement = 256,
JsxOpeningElement = 257,
JsxClosingElement = 258,
JsxFragment = 259,
JsxOpeningFragment = 260,
JsxClosingFragment = 261,
JsxAttribute = 262,
JsxAttributes = 263,
JsxSpreadAttribute = 264,
JsxExpression = 265,
CaseClause = 266,
DefaultClause = 267,
HeritageClause = 268,
CatchClause = 269,
PropertyAssignment = 270,
ShorthandPropertyAssignment = 271,
SpreadAssignment = 272,
EnumMember = 273,
SourceFile = 274,
Bundle = 275,
UnparsedSource = 276,
InputFiles = 277,
JSDocTypeExpression = 278,
JSDocAllType = 279,
JSDocUnknownType = 280,
JSDocNullableType = 281,
JSDocNonNullableType = 282,
JSDocOptionalType = 283,
JSDocFunctionType = 284,
JSDocVariadicType = 285,
JSDocComment = 286,
JSDocTypeLiteral = 287,
JSDocSignature = 288,
JSDocTag = 289,
JSDocAugmentsTag = 290,
JSDocClassTag = 291,
JSDocCallbackTag = 292,
JSDocParameterTag = 293,
JSDocReturnTag = 294,
JSDocTypeTag = 295,
JSDocTemplateTag = 296,
JSDocTypedefTag = 297,
JSDocPropertyTag = 298,
SyntaxList = 299,
NotEmittedStatement = 300,
PartiallyEmittedExpression = 301,
CommaListExpression = 302,
MergeDeclarationMarker = 303,
EndOfDeclarationMarker = 304,
Count = 305,
FirstAssignment = 58,
LastAssignment = 70,
FirstCompoundAssignment = 59,
@ -374,15 +375,15 @@ declare namespace ts {
FirstReservedWord = 72,
LastReservedWord = 107,
FirstKeyword = 72,
LastKeyword = 144,
LastKeyword = 145,
FirstFutureReservedWord = 108,
LastFutureReservedWord = 116,
FirstTypeNode = 160,
LastTypeNode = 178,
FirstTypeNode = 161,
LastTypeNode = 179,
FirstPunctuation = 17,
LastPunctuation = 70,
FirstToken = 0,
LastToken = 144,
LastToken = 145,
FirstTriviaToken = 2,
LastTriviaToken = 7,
FirstLiteralToken = 8,
@ -391,11 +392,11 @@ declare namespace ts {
LastTemplateToken = 16,
FirstBinaryOperator = 27,
LastBinaryOperator = 70,
FirstNode = 145,
FirstJSDocNode = 277,
LastJSDocNode = 297,
FirstJSDocTagNode = 288,
LastJSDocTagNode = 297
FirstNode = 146,
FirstJSDocNode = 278,
LastJSDocNode = 298,
FirstJSDocTagNode = 289,
LastJSDocTagNode = 298
}
enum NodeFlags {
None = 0,
@ -702,7 +703,7 @@ declare namespace ts {
_typeNodeBrand: any;
}
interface KeywordTypeNode extends TypeNode {
kind: SyntaxKind.AnyKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.StringKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.ThisKeyword | SyntaxKind.VoidKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.NullKeyword | SyntaxKind.NeverKeyword;
kind: SyntaxKind.AnyKeyword | SyntaxKind.UnknownKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.StringKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.ThisKeyword | SyntaxKind.VoidKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.NullKeyword | SyntaxKind.NeverKeyword;
}
interface ImportTypeNode extends NodeWithTypeArguments {
kind: SyntaxKind.ImportType;
@ -2128,48 +2129,49 @@ declare namespace ts {
type SymbolTable = UnderscoreEscapedMap<Symbol>;
enum TypeFlags {
Any = 1,
String = 2,
Number = 4,
Boolean = 8,
Enum = 16,
StringLiteral = 32,
NumberLiteral = 64,
BooleanLiteral = 128,
EnumLiteral = 256,
ESSymbol = 512,
UniqueESSymbol = 1024,
Void = 2048,
Undefined = 4096,
Null = 8192,
Never = 16384,
TypeParameter = 32768,
Object = 65536,
Union = 131072,
Intersection = 262144,
Index = 524288,
IndexedAccess = 1048576,
Conditional = 2097152,
Substitution = 4194304,
NonPrimitive = 134217728,
Literal = 224,
Unit = 13536,
StringOrNumberLiteral = 96,
PossiblyFalsy = 14574,
StringLike = 34,
NumberLike = 84,
BooleanLike = 136,
EnumLike = 272,
ESSymbolLike = 1536,
VoidLike = 6144,
UnionOrIntersection = 393216,
StructuredType = 458752,
TypeVariable = 1081344,
InstantiableNonPrimitive = 7372800,
InstantiablePrimitive = 524288,
Instantiable = 7897088,
StructuredOrInstantiable = 8355840,
Narrowable = 142575359,
NotUnionOrUnit = 134283777
Unknown = 2,
String = 4,
Number = 8,
Boolean = 16,
Enum = 32,
StringLiteral = 64,
NumberLiteral = 128,
BooleanLiteral = 256,
EnumLiteral = 512,
ESSymbol = 1024,
UniqueESSymbol = 2048,
Void = 4096,
Undefined = 8192,
Null = 16384,
Never = 32768,
TypeParameter = 65536,
Object = 131072,
Union = 262144,
Intersection = 524288,
Index = 1048576,
IndexedAccess = 2097152,
Conditional = 4194304,
Substitution = 8388608,
NonPrimitive = 16777216,
Literal = 448,
Unit = 27072,
StringOrNumberLiteral = 192,
PossiblyFalsy = 29148,
StringLike = 68,
NumberLike = 168,
BooleanLike = 272,
EnumLike = 544,
ESSymbolLike = 3072,
VoidLike = 12288,
UnionOrIntersection = 786432,
StructuredType = 917504,
TypeVariable = 2162688,
InstantiableNonPrimitive = 14745600,
InstantiablePrimitive = 1048576,
Instantiable = 15794176,
StructuredOrInstantiable = 16711680,
Narrowable = 33492479,
NotUnionOrUnit = 16909315
}
type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;
interface Type {

View File

@ -0,0 +1,233 @@
tests/cases/conformance/types/unknown/unknownType1.ts(50,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(51,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(52,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(53,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(54,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(55,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(56,6): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(57,6): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(63,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(64,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(65,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(66,9): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(110,9): error TS2322: Type 'unknown' is not assignable to type 'object'.
tests/cases/conformance/types/unknown/unknownType1.ts(111,9): error TS2322: Type 'unknown' is not assignable to type 'string'.
tests/cases/conformance/types/unknown/unknownType1.ts(112,9): error TS2322: Type 'unknown' is not assignable to type 'string[]'.
tests/cases/conformance/types/unknown/unknownType1.ts(113,9): error TS2322: Type 'unknown' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(114,9): error TS2322: Type 'unknown' is not assignable to type '{} | null | undefined'.
Type 'unknown' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(120,9): error TS2322: Type 'T' is not assignable to type 'object'.
tests/cases/conformance/types/unknown/unknownType1.ts(129,5): error TS2322: Type '123' is not assignable to type '{ [x: string]: unknown; }'.
tests/cases/conformance/types/unknown/unknownType1.ts(155,14): error TS2700: Rest types may only be created from object types.
tests/cases/conformance/types/unknown/unknownType1.ts(161,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
==== tests/cases/conformance/types/unknown/unknownType1.ts (21 errors) ====
// In an intersection everything absorbs unknown
type T00 = unknown & null; // null
type T01 = unknown & undefined; // undefined
type T02 = unknown & null & undefined; // null & undefined (which becomes never in union)
type T03 = unknown & string; // string
type T04 = unknown & string[]; // string[]
type T05 = unknown & unknown; // unknown
type T06 = unknown & any; // any
// In a union an unknown absorbs everything
type T10 = unknown | null; // unknown
type T11 = unknown | undefined; // unknown
type T12 = unknown | null | undefined; // unknown
type T13 = unknown | string; // unknown
type T14 = unknown | string[]; // unknown
type T15 = unknown | unknown; // unknown
type T16 = unknown | any; // any
// Type variable and unknown in union and intersection
type T20<T> = T & {}; // T & {}
type T21<T> = T | {}; // T | {}
type T22<T> = T & unknown; // T
type T23<T> = T | unknown; // unknown
// unknown in conditional types
type T30<T> = unknown extends T ? true : false; // Deferred
type T31<T> = T extends unknown ? true : false; // Deferred (so it distributes)
type T32<T> = never extends T ? true : false; // true
type T33<T> = T extends never ? true : false; // Deferred
type T35<T> = T extends unknown ? { x: T } : false;
type T36 = T35<string | number>; // { x: string } | { x: number }
type T37 = T35<any>; // { x: any }
type T38 = T35<unknown>; // { x: unknown }
// keyof unknown
type T40 = keyof any; // string | number | symbol
type T41 = keyof unknown; // never
// Only equality operators are allowed with unknown
function f10(x: unknown) {
x == 5;
x !== 10;
x >= 0; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x.foo; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x[10]; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x(); // Error
~
!!! error TS2571: Object is of type 'unknown'.
x + 1; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x * 2; // Error
~
!!! error TS2571: Object is of type 'unknown'.
-x; // Error
~
!!! error TS2571: Object is of type 'unknown'.
+x; // Error
~
!!! error TS2571: Object is of type 'unknown'.
}
// No property accesses, element accesses, or function calls
function f11(x: unknown) {
x.foo; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x[5]; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x(); // Error
~
!!! error TS2571: Object is of type 'unknown'.
new x(); // Error
~
!!! error TS2571: Object is of type 'unknown'.
}
// typeof, instanceof, and user defined type predicates
declare function isFunction(x: unknown): x is Function;
function f20(x: unknown) {
if (typeof x === "string" || typeof x === "number") {
x; // string | number
}
if (x instanceof Error) {
x; // Error
}
if (isFunction(x)) {
x; // Function
}
}
// Homomorphic mapped type over unknown
type T50<T> = { [P in keyof T]: number };
type T51 = T50<any>; // { [x: string]: number }
type T52 = T50<unknown>; // {}
// Anything is assignable to unknown
function f21<T>(pAny: any, pNever: never, pT: T) {
let x: unknown;
x = 123;
x = "hello";
x = [1, 2, 3];
x = new Error();
x = x;
x = pAny;
x = pNever;
x = pT;
}
// unknown assignable only to itself and any
function f22(x: unknown) {
let v1: any = x;
let v2: unknown = x;
let v3: object = x; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type 'object'.
let v4: string = x; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type 'string'.
let v5: string[] = x; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type 'string[]'.
let v6: {} = x; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type '{}'.
let v7: {} | null | undefined = x; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type '{} | null | undefined'.
!!! error TS2322: Type 'unknown' is not assignable to type '{}'.
}
// Type parameter 'T extends unknown' not related to object
function f23<T extends unknown>(x: T) {
let y: object = x; // Error
~
!!! error TS2322: Type 'T' is not assignable to type 'object'.
}
// Anything but primitive assignable to { [x: string]: unknown }
function f24(x: { [x: string]: unknown }) {
x = {};
x = { a: 5 };
x = [1, 2, 3];
x = 123; // Error
~
!!! error TS2322: Type '123' is not assignable to type '{ [x: string]: unknown; }'.
}
// Locals of type unknown always considered initialized
function f25() {
let x: unknown;
let y = x;
}
// Spread of unknown causes result to be unknown
function f26(x: {}, y: unknown, z: any) {
let o1 = { a: 42, ...x }; // { a: number }
let o2 = { a: 42, ...x, ...y }; // unknown
let o3 = { a: 42, ...x, ...y, ...z }; // any
}
// Functions with unknown return type don't need return expressions
function f27(): unknown {
}
// Rest type cannot be created from unknown
function f28(x: unknown) {
let { ...a } = x; // Error
~
!!! error TS2700: Rest types may only be created from object types.
}
// Class properties of type unknown don't need definite assignment
class C1 {
a: string; // Error
~
!!! error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
b: unknown;
c: any;
}

View File

@ -0,0 +1,275 @@
//// [unknownType1.ts]
// In an intersection everything absorbs unknown
type T00 = unknown & null; // null
type T01 = unknown & undefined; // undefined
type T02 = unknown & null & undefined; // null & undefined (which becomes never in union)
type T03 = unknown & string; // string
type T04 = unknown & string[]; // string[]
type T05 = unknown & unknown; // unknown
type T06 = unknown & any; // any
// In a union an unknown absorbs everything
type T10 = unknown | null; // unknown
type T11 = unknown | undefined; // unknown
type T12 = unknown | null | undefined; // unknown
type T13 = unknown | string; // unknown
type T14 = unknown | string[]; // unknown
type T15 = unknown | unknown; // unknown
type T16 = unknown | any; // any
// Type variable and unknown in union and intersection
type T20<T> = T & {}; // T & {}
type T21<T> = T | {}; // T | {}
type T22<T> = T & unknown; // T
type T23<T> = T | unknown; // unknown
// unknown in conditional types
type T30<T> = unknown extends T ? true : false; // Deferred
type T31<T> = T extends unknown ? true : false; // Deferred (so it distributes)
type T32<T> = never extends T ? true : false; // true
type T33<T> = T extends never ? true : false; // Deferred
type T35<T> = T extends unknown ? { x: T } : false;
type T36 = T35<string | number>; // { x: string } | { x: number }
type T37 = T35<any>; // { x: any }
type T38 = T35<unknown>; // { x: unknown }
// keyof unknown
type T40 = keyof any; // string | number | symbol
type T41 = keyof unknown; // never
// Only equality operators are allowed with unknown
function f10(x: unknown) {
x == 5;
x !== 10;
x >= 0; // Error
x.foo; // Error
x[10]; // Error
x(); // Error
x + 1; // Error
x * 2; // Error
-x; // Error
+x; // Error
}
// No property accesses, element accesses, or function calls
function f11(x: unknown) {
x.foo; // Error
x[5]; // Error
x(); // Error
new x(); // Error
}
// typeof, instanceof, and user defined type predicates
declare function isFunction(x: unknown): x is Function;
function f20(x: unknown) {
if (typeof x === "string" || typeof x === "number") {
x; // string | number
}
if (x instanceof Error) {
x; // Error
}
if (isFunction(x)) {
x; // Function
}
}
// Homomorphic mapped type over unknown
type T50<T> = { [P in keyof T]: number };
type T51 = T50<any>; // { [x: string]: number }
type T52 = T50<unknown>; // {}
// Anything is assignable to unknown
function f21<T>(pAny: any, pNever: never, pT: T) {
let x: unknown;
x = 123;
x = "hello";
x = [1, 2, 3];
x = new Error();
x = x;
x = pAny;
x = pNever;
x = pT;
}
// unknown assignable only to itself and any
function f22(x: unknown) {
let v1: any = x;
let v2: unknown = x;
let v3: object = x; // Error
let v4: string = x; // Error
let v5: string[] = x; // Error
let v6: {} = x; // Error
let v7: {} | null | undefined = x; // Error
}
// Type parameter 'T extends unknown' not related to object
function f23<T extends unknown>(x: T) {
let y: object = x; // Error
}
// Anything but primitive assignable to { [x: string]: unknown }
function f24(x: { [x: string]: unknown }) {
x = {};
x = { a: 5 };
x = [1, 2, 3];
x = 123; // Error
}
// Locals of type unknown always considered initialized
function f25() {
let x: unknown;
let y = x;
}
// Spread of unknown causes result to be unknown
function f26(x: {}, y: unknown, z: any) {
let o1 = { a: 42, ...x }; // { a: number }
let o2 = { a: 42, ...x, ...y }; // unknown
let o3 = { a: 42, ...x, ...y, ...z }; // any
}
// Functions with unknown return type don't need return expressions
function f27(): unknown {
}
// Rest type cannot be created from unknown
function f28(x: unknown) {
let { ...a } = x; // Error
}
// Class properties of type unknown don't need definite assignment
class C1 {
a: string; // Error
b: unknown;
c: any;
}
//// [unknownType1.js]
"use strict";
// In an intersection everything absorbs unknown
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
// Only equality operators are allowed with unknown
function f10(x) {
x == 5;
x !== 10;
x >= 0; // Error
x.foo; // Error
x[10]; // Error
x(); // Error
x + 1; // Error
x * 2; // Error
-x; // Error
+x; // Error
}
// No property accesses, element accesses, or function calls
function f11(x) {
x.foo; // Error
x[5]; // Error
x(); // Error
new x(); // Error
}
function f20(x) {
if (typeof x === "string" || typeof x === "number") {
x; // string | number
}
if (x instanceof Error) {
x; // Error
}
if (isFunction(x)) {
x; // Function
}
}
// Anything is assignable to unknown
function f21(pAny, pNever, pT) {
var x;
x = 123;
x = "hello";
x = [1, 2, 3];
x = new Error();
x = x;
x = pAny;
x = pNever;
x = pT;
}
// unknown assignable only to itself and any
function f22(x) {
var v1 = x;
var v2 = x;
var v3 = x; // Error
var v4 = x; // Error
var v5 = x; // Error
var v6 = x; // Error
var v7 = x; // Error
}
// Type parameter 'T extends unknown' not related to object
function f23(x) {
var y = x; // Error
}
// Anything but primitive assignable to { [x: string]: unknown }
function f24(x) {
x = {};
x = { a: 5 };
x = [1, 2, 3];
x = 123; // Error
}
// Locals of type unknown always considered initialized
function f25() {
var x;
var y = x;
}
// Spread of unknown causes result to be unknown
function f26(x, y, z) {
var o1 = __assign({ a: 42 }, x); // { a: number }
var o2 = __assign({ a: 42 }, x, y); // unknown
var o3 = __assign({ a: 42 }, x, y, z); // any
}
// Functions with unknown return type don't need return expressions
function f27() {
}
// Rest type cannot be created from unknown
function f28(x) {
var a = __rest(x, []); // Error
}
// Class properties of type unknown don't need definite assignment
var C1 = /** @class */ (function () {
function C1() {
}
return C1;
}());

View File

@ -0,0 +1,409 @@
=== tests/cases/conformance/types/unknown/unknownType1.ts ===
// In an intersection everything absorbs unknown
type T00 = unknown & null; // null
>T00 : Symbol(T00, Decl(unknownType1.ts, 0, 0))
type T01 = unknown & undefined; // undefined
>T01 : Symbol(T01, Decl(unknownType1.ts, 2, 26))
type T02 = unknown & null & undefined; // null & undefined (which becomes never in union)
>T02 : Symbol(T02, Decl(unknownType1.ts, 3, 31))
type T03 = unknown & string; // string
>T03 : Symbol(T03, Decl(unknownType1.ts, 4, 38))
type T04 = unknown & string[]; // string[]
>T04 : Symbol(T04, Decl(unknownType1.ts, 5, 28))
type T05 = unknown & unknown; // unknown
>T05 : Symbol(T05, Decl(unknownType1.ts, 6, 30))
type T06 = unknown & any; // any
>T06 : Symbol(T06, Decl(unknownType1.ts, 7, 29))
// In a union an unknown absorbs everything
type T10 = unknown | null; // unknown
>T10 : Symbol(T10, Decl(unknownType1.ts, 8, 25))
type T11 = unknown | undefined; // unknown
>T11 : Symbol(T11, Decl(unknownType1.ts, 12, 26))
type T12 = unknown | null | undefined; // unknown
>T12 : Symbol(T12, Decl(unknownType1.ts, 13, 31))
type T13 = unknown | string; // unknown
>T13 : Symbol(T13, Decl(unknownType1.ts, 14, 38))
type T14 = unknown | string[]; // unknown
>T14 : Symbol(T14, Decl(unknownType1.ts, 15, 28))
type T15 = unknown | unknown; // unknown
>T15 : Symbol(T15, Decl(unknownType1.ts, 16, 30))
type T16 = unknown | any; // any
>T16 : Symbol(T16, Decl(unknownType1.ts, 17, 29))
// Type variable and unknown in union and intersection
type T20<T> = T & {}; // T & {}
>T20 : Symbol(T20, Decl(unknownType1.ts, 18, 25))
>T : Symbol(T, Decl(unknownType1.ts, 22, 9))
>T : Symbol(T, Decl(unknownType1.ts, 22, 9))
type T21<T> = T | {}; // T | {}
>T21 : Symbol(T21, Decl(unknownType1.ts, 22, 21))
>T : Symbol(T, Decl(unknownType1.ts, 23, 9))
>T : Symbol(T, Decl(unknownType1.ts, 23, 9))
type T22<T> = T & unknown; // T
>T22 : Symbol(T22, Decl(unknownType1.ts, 23, 21))
>T : Symbol(T, Decl(unknownType1.ts, 24, 9))
>T : Symbol(T, Decl(unknownType1.ts, 24, 9))
type T23<T> = T | unknown; // unknown
>T23 : Symbol(T23, Decl(unknownType1.ts, 24, 26))
>T : Symbol(T, Decl(unknownType1.ts, 25, 9))
>T : Symbol(T, Decl(unknownType1.ts, 25, 9))
// unknown in conditional types
type T30<T> = unknown extends T ? true : false; // Deferred
>T30 : Symbol(T30, Decl(unknownType1.ts, 25, 26))
>T : Symbol(T, Decl(unknownType1.ts, 29, 9))
>T : Symbol(T, Decl(unknownType1.ts, 29, 9))
type T31<T> = T extends unknown ? true : false; // Deferred (so it distributes)
>T31 : Symbol(T31, Decl(unknownType1.ts, 29, 47))
>T : Symbol(T, Decl(unknownType1.ts, 30, 9))
>T : Symbol(T, Decl(unknownType1.ts, 30, 9))
type T32<T> = never extends T ? true : false; // true
>T32 : Symbol(T32, Decl(unknownType1.ts, 30, 47))
>T : Symbol(T, Decl(unknownType1.ts, 31, 9))
>T : Symbol(T, Decl(unknownType1.ts, 31, 9))
type T33<T> = T extends never ? true : false; // Deferred
>T33 : Symbol(T33, Decl(unknownType1.ts, 31, 45))
>T : Symbol(T, Decl(unknownType1.ts, 32, 9))
>T : Symbol(T, Decl(unknownType1.ts, 32, 9))
type T35<T> = T extends unknown ? { x: T } : false;
>T35 : Symbol(T35, Decl(unknownType1.ts, 32, 45))
>T : Symbol(T, Decl(unknownType1.ts, 34, 9))
>T : Symbol(T, Decl(unknownType1.ts, 34, 9))
>x : Symbol(x, Decl(unknownType1.ts, 34, 35))
>T : Symbol(T, Decl(unknownType1.ts, 34, 9))
type T36 = T35<string | number>; // { x: string } | { x: number }
>T36 : Symbol(T36, Decl(unknownType1.ts, 34, 51))
>T35 : Symbol(T35, Decl(unknownType1.ts, 32, 45))
type T37 = T35<any>; // { x: any }
>T37 : Symbol(T37, Decl(unknownType1.ts, 35, 32))
>T35 : Symbol(T35, Decl(unknownType1.ts, 32, 45))
type T38 = T35<unknown>; // { x: unknown }
>T38 : Symbol(T38, Decl(unknownType1.ts, 36, 20))
>T35 : Symbol(T35, Decl(unknownType1.ts, 32, 45))
// keyof unknown
type T40 = keyof any; // string | number | symbol
>T40 : Symbol(T40, Decl(unknownType1.ts, 37, 24))
type T41 = keyof unknown; // never
>T41 : Symbol(T41, Decl(unknownType1.ts, 41, 21))
// Only equality operators are allowed with unknown
function f10(x: unknown) {
>f10 : Symbol(f10, Decl(unknownType1.ts, 42, 25))
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
x == 5;
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
x !== 10;
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
x >= 0; // Error
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
x.foo; // Error
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
x[10]; // Error
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
x(); // Error
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
x + 1; // Error
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
x * 2; // Error
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
-x; // Error
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
+x; // Error
>x : Symbol(x, Decl(unknownType1.ts, 46, 13))
}
// No property accesses, element accesses, or function calls
function f11(x: unknown) {
>f11 : Symbol(f11, Decl(unknownType1.ts, 57, 1))
>x : Symbol(x, Decl(unknownType1.ts, 61, 13))
x.foo; // Error
>x : Symbol(x, Decl(unknownType1.ts, 61, 13))
x[5]; // Error
>x : Symbol(x, Decl(unknownType1.ts, 61, 13))
x(); // Error
>x : Symbol(x, Decl(unknownType1.ts, 61, 13))
new x(); // Error
>x : Symbol(x, Decl(unknownType1.ts, 61, 13))
}
// typeof, instanceof, and user defined type predicates
declare function isFunction(x: unknown): x is Function;
>isFunction : Symbol(isFunction, Decl(unknownType1.ts, 66, 1))
>x : Symbol(x, Decl(unknownType1.ts, 70, 28))
>x : Symbol(x, Decl(unknownType1.ts, 70, 28))
>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
function f20(x: unknown) {
>f20 : Symbol(f20, Decl(unknownType1.ts, 70, 55))
>x : Symbol(x, Decl(unknownType1.ts, 72, 13))
if (typeof x === "string" || typeof x === "number") {
>x : Symbol(x, Decl(unknownType1.ts, 72, 13))
>x : Symbol(x, Decl(unknownType1.ts, 72, 13))
x; // string | number
>x : Symbol(x, Decl(unknownType1.ts, 72, 13))
}
if (x instanceof Error) {
>x : Symbol(x, Decl(unknownType1.ts, 72, 13))
>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
x; // Error
>x : Symbol(x, Decl(unknownType1.ts, 72, 13))
}
if (isFunction(x)) {
>isFunction : Symbol(isFunction, Decl(unknownType1.ts, 66, 1))
>x : Symbol(x, Decl(unknownType1.ts, 72, 13))
x; // Function
>x : Symbol(x, Decl(unknownType1.ts, 72, 13))
}
}
// Homomorphic mapped type over unknown
type T50<T> = { [P in keyof T]: number };
>T50 : Symbol(T50, Decl(unknownType1.ts, 82, 1))
>T : Symbol(T, Decl(unknownType1.ts, 86, 9))
>P : Symbol(P, Decl(unknownType1.ts, 86, 17))
>T : Symbol(T, Decl(unknownType1.ts, 86, 9))
type T51 = T50<any>; // { [x: string]: number }
>T51 : Symbol(T51, Decl(unknownType1.ts, 86, 41))
>T50 : Symbol(T50, Decl(unknownType1.ts, 82, 1))
type T52 = T50<unknown>; // {}
>T52 : Symbol(T52, Decl(unknownType1.ts, 87, 20))
>T50 : Symbol(T50, Decl(unknownType1.ts, 82, 1))
// Anything is assignable to unknown
function f21<T>(pAny: any, pNever: never, pT: T) {
>f21 : Symbol(f21, Decl(unknownType1.ts, 88, 24))
>T : Symbol(T, Decl(unknownType1.ts, 92, 13))
>pAny : Symbol(pAny, Decl(unknownType1.ts, 92, 16))
>pNever : Symbol(pNever, Decl(unknownType1.ts, 92, 26))
>pT : Symbol(pT, Decl(unknownType1.ts, 92, 41))
>T : Symbol(T, Decl(unknownType1.ts, 92, 13))
let x: unknown;
>x : Symbol(x, Decl(unknownType1.ts, 93, 7))
x = 123;
>x : Symbol(x, Decl(unknownType1.ts, 93, 7))
x = "hello";
>x : Symbol(x, Decl(unknownType1.ts, 93, 7))
x = [1, 2, 3];
>x : Symbol(x, Decl(unknownType1.ts, 93, 7))
x = new Error();
>x : Symbol(x, Decl(unknownType1.ts, 93, 7))
>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
x = x;
>x : Symbol(x, Decl(unknownType1.ts, 93, 7))
>x : Symbol(x, Decl(unknownType1.ts, 93, 7))
x = pAny;
>x : Symbol(x, Decl(unknownType1.ts, 93, 7))
>pAny : Symbol(pAny, Decl(unknownType1.ts, 92, 16))
x = pNever;
>x : Symbol(x, Decl(unknownType1.ts, 93, 7))
>pNever : Symbol(pNever, Decl(unknownType1.ts, 92, 26))
x = pT;
>x : Symbol(x, Decl(unknownType1.ts, 93, 7))
>pT : Symbol(pT, Decl(unknownType1.ts, 92, 41))
}
// unknown assignable only to itself and any
function f22(x: unknown) {
>f22 : Symbol(f22, Decl(unknownType1.ts, 102, 1))
>x : Symbol(x, Decl(unknownType1.ts, 106, 13))
let v1: any = x;
>v1 : Symbol(v1, Decl(unknownType1.ts, 107, 7))
>x : Symbol(x, Decl(unknownType1.ts, 106, 13))
let v2: unknown = x;
>v2 : Symbol(v2, Decl(unknownType1.ts, 108, 7))
>x : Symbol(x, Decl(unknownType1.ts, 106, 13))
let v3: object = x; // Error
>v3 : Symbol(v3, Decl(unknownType1.ts, 109, 7))
>x : Symbol(x, Decl(unknownType1.ts, 106, 13))
let v4: string = x; // Error
>v4 : Symbol(v4, Decl(unknownType1.ts, 110, 7))
>x : Symbol(x, Decl(unknownType1.ts, 106, 13))
let v5: string[] = x; // Error
>v5 : Symbol(v5, Decl(unknownType1.ts, 111, 7))
>x : Symbol(x, Decl(unknownType1.ts, 106, 13))
let v6: {} = x; // Error
>v6 : Symbol(v6, Decl(unknownType1.ts, 112, 7))
>x : Symbol(x, Decl(unknownType1.ts, 106, 13))
let v7: {} | null | undefined = x; // Error
>v7 : Symbol(v7, Decl(unknownType1.ts, 113, 7))
>x : Symbol(x, Decl(unknownType1.ts, 106, 13))
}
// Type parameter 'T extends unknown' not related to object
function f23<T extends unknown>(x: T) {
>f23 : Symbol(f23, Decl(unknownType1.ts, 114, 1))
>T : Symbol(T, Decl(unknownType1.ts, 118, 13))
>x : Symbol(x, Decl(unknownType1.ts, 118, 32))
>T : Symbol(T, Decl(unknownType1.ts, 118, 13))
let y: object = x; // Error
>y : Symbol(y, Decl(unknownType1.ts, 119, 7))
>x : Symbol(x, Decl(unknownType1.ts, 118, 32))
}
// Anything but primitive assignable to { [x: string]: unknown }
function f24(x: { [x: string]: unknown }) {
>f24 : Symbol(f24, Decl(unknownType1.ts, 120, 1))
>x : Symbol(x, Decl(unknownType1.ts, 124, 13))
>x : Symbol(x, Decl(unknownType1.ts, 124, 19))
x = {};
>x : Symbol(x, Decl(unknownType1.ts, 124, 13))
x = { a: 5 };
>x : Symbol(x, Decl(unknownType1.ts, 124, 13))
>a : Symbol(a, Decl(unknownType1.ts, 126, 9))
x = [1, 2, 3];
>x : Symbol(x, Decl(unknownType1.ts, 124, 13))
x = 123; // Error
>x : Symbol(x, Decl(unknownType1.ts, 124, 13))
}
// Locals of type unknown always considered initialized
function f25() {
>f25 : Symbol(f25, Decl(unknownType1.ts, 129, 1))
let x: unknown;
>x : Symbol(x, Decl(unknownType1.ts, 134, 7))
let y = x;
>y : Symbol(y, Decl(unknownType1.ts, 135, 7))
>x : Symbol(x, Decl(unknownType1.ts, 134, 7))
}
// Spread of unknown causes result to be unknown
function f26(x: {}, y: unknown, z: any) {
>f26 : Symbol(f26, Decl(unknownType1.ts, 136, 1))
>x : Symbol(x, Decl(unknownType1.ts, 140, 13))
>y : Symbol(y, Decl(unknownType1.ts, 140, 19))
>z : Symbol(z, Decl(unknownType1.ts, 140, 31))
let o1 = { a: 42, ...x }; // { a: number }
>o1 : Symbol(o1, Decl(unknownType1.ts, 141, 7))
>a : Symbol(a, Decl(unknownType1.ts, 141, 14))
>x : Symbol(x, Decl(unknownType1.ts, 140, 13))
let o2 = { a: 42, ...x, ...y }; // unknown
>o2 : Symbol(o2, Decl(unknownType1.ts, 142, 7))
>a : Symbol(a, Decl(unknownType1.ts, 142, 14))
>x : Symbol(x, Decl(unknownType1.ts, 140, 13))
>y : Symbol(y, Decl(unknownType1.ts, 140, 19))
let o3 = { a: 42, ...x, ...y, ...z }; // any
>o3 : Symbol(o3, Decl(unknownType1.ts, 143, 7))
>a : Symbol(a, Decl(unknownType1.ts, 143, 14))
>x : Symbol(x, Decl(unknownType1.ts, 140, 13))
>y : Symbol(y, Decl(unknownType1.ts, 140, 19))
>z : Symbol(z, Decl(unknownType1.ts, 140, 31))
}
// Functions with unknown return type don't need return expressions
function f27(): unknown {
>f27 : Symbol(f27, Decl(unknownType1.ts, 144, 1))
}
// Rest type cannot be created from unknown
function f28(x: unknown) {
>f28 : Symbol(f28, Decl(unknownType1.ts, 149, 1))
>x : Symbol(x, Decl(unknownType1.ts, 153, 13))
let { ...a } = x; // Error
>a : Symbol(a, Decl(unknownType1.ts, 154, 9))
>x : Symbol(x, Decl(unknownType1.ts, 153, 13))
}
// Class properties of type unknown don't need definite assignment
class C1 {
>C1 : Symbol(C1, Decl(unknownType1.ts, 155, 1))
a: string; // Error
>a : Symbol(C1.a, Decl(unknownType1.ts, 159, 10))
b: unknown;
>b : Symbol(C1.b, Decl(unknownType1.ts, 160, 14))
c: any;
>c : Symbol(C1.c, Decl(unknownType1.ts, 161, 15))
}

View File

@ -0,0 +1,488 @@
=== tests/cases/conformance/types/unknown/unknownType1.ts ===
// In an intersection everything absorbs unknown
type T00 = unknown & null; // null
>T00 : null
>null : null
type T01 = unknown & undefined; // undefined
>T01 : undefined
type T02 = unknown & null & undefined; // null & undefined (which becomes never in union)
>T02 : T02
>null : null
type T03 = unknown & string; // string
>T03 : string
type T04 = unknown & string[]; // string[]
>T04 : string[]
type T05 = unknown & unknown; // unknown
>T05 : unknown
type T06 = unknown & any; // any
>T06 : any
// In a union an unknown absorbs everything
type T10 = unknown | null; // unknown
>T10 : unknown
>null : null
type T11 = unknown | undefined; // unknown
>T11 : unknown
type T12 = unknown | null | undefined; // unknown
>T12 : unknown
>null : null
type T13 = unknown | string; // unknown
>T13 : unknown
type T14 = unknown | string[]; // unknown
>T14 : unknown
type T15 = unknown | unknown; // unknown
>T15 : unknown
type T16 = unknown | any; // any
>T16 : any
// Type variable and unknown in union and intersection
type T20<T> = T & {}; // T & {}
>T20 : T20<T>
>T : T
>T : T
type T21<T> = T | {}; // T | {}
>T21 : T21<T>
>T : T
>T : T
type T22<T> = T & unknown; // T
>T22 : T
>T : T
>T : T
type T23<T> = T | unknown; // unknown
>T23 : unknown
>T : T
>T : T
// unknown in conditional types
type T30<T> = unknown extends T ? true : false; // Deferred
>T30 : T30<T>
>T : T
>T : T
>true : true
>false : false
type T31<T> = T extends unknown ? true : false; // Deferred (so it distributes)
>T31 : T31<T>
>T : T
>T : T
>true : true
>false : false
type T32<T> = never extends T ? true : false; // true
>T32 : true
>T : T
>T : T
>true : true
>false : false
type T33<T> = T extends never ? true : false; // Deferred
>T33 : T33<T>
>T : T
>T : T
>true : true
>false : false
type T35<T> = T extends unknown ? { x: T } : false;
>T35 : T35<T>
>T : T
>T : T
>x : T
>T : T
>false : false
type T36 = T35<string | number>; // { x: string } | { x: number }
>T36 : { x: string; } | { x: number; }
>T35 : T35<T>
type T37 = T35<any>; // { x: any }
>T37 : { x: any; }
>T35 : T35<T>
type T38 = T35<unknown>; // { x: unknown }
>T38 : { x: unknown; }
>T35 : T35<T>
// keyof unknown
type T40 = keyof any; // string | number | symbol
>T40 : string | number | symbol
type T41 = keyof unknown; // never
>T41 : never
// Only equality operators are allowed with unknown
function f10(x: unknown) {
>f10 : (x: unknown) => void
>x : unknown
x == 5;
>x == 5 : boolean
>x : unknown
>5 : 5
x !== 10;
>x !== 10 : boolean
>x : unknown
>10 : 10
x >= 0; // Error
>x >= 0 : boolean
>x : unknown
>0 : 0
x.foo; // Error
>x.foo : any
>x : unknown
>foo : any
x[10]; // Error
>x[10] : any
>x : unknown
>10 : 10
x(); // Error
>x() : any
>x : unknown
x + 1; // Error
>x + 1 : any
>x : unknown
>1 : 1
x * 2; // Error
>x * 2 : number
>x : unknown
>2 : 2
-x; // Error
>-x : number
>x : unknown
+x; // Error
>+x : number
>x : unknown
}
// No property accesses, element accesses, or function calls
function f11(x: unknown) {
>f11 : (x: unknown) => void
>x : unknown
x.foo; // Error
>x.foo : any
>x : unknown
>foo : any
x[5]; // Error
>x[5] : any
>x : unknown
>5 : 5
x(); // Error
>x() : any
>x : unknown
new x(); // Error
>new x() : any
>x : unknown
}
// typeof, instanceof, and user defined type predicates
declare function isFunction(x: unknown): x is Function;
>isFunction : (x: unknown) => x is Function
>x : unknown
>x : any
>Function : Function
function f20(x: unknown) {
>f20 : (x: unknown) => void
>x : unknown
if (typeof x === "string" || typeof x === "number") {
>typeof x === "string" || typeof x === "number" : boolean
>typeof x === "string" : boolean
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>"string" : "string"
>typeof x === "number" : boolean
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>"number" : "number"
x; // string | number
>x : string | number
}
if (x instanceof Error) {
>x instanceof Error : boolean
>x : unknown
>Error : ErrorConstructor
x; // Error
>x : Error
}
if (isFunction(x)) {
>isFunction(x) : boolean
>isFunction : (x: unknown) => x is Function
>x : unknown
x; // Function
>x : Function
}
}
// Homomorphic mapped type over unknown
type T50<T> = { [P in keyof T]: number };
>T50 : T50<T>
>T : T
>P : P
>T : T
type T51 = T50<any>; // { [x: string]: number }
>T51 : T50<any>
>T50 : T50<T>
type T52 = T50<unknown>; // {}
>T52 : T50<unknown>
>T50 : T50<T>
// Anything is assignable to unknown
function f21<T>(pAny: any, pNever: never, pT: T) {
>f21 : <T>(pAny: any, pNever: never, pT: T) => void
>T : T
>pAny : any
>pNever : never
>pT : T
>T : T
let x: unknown;
>x : unknown
x = 123;
>x = 123 : 123
>x : unknown
>123 : 123
x = "hello";
>x = "hello" : "hello"
>x : unknown
>"hello" : "hello"
x = [1, 2, 3];
>x = [1, 2, 3] : number[]
>x : unknown
>[1, 2, 3] : number[]
>1 : 1
>2 : 2
>3 : 3
x = new Error();
>x = new Error() : Error
>x : unknown
>new Error() : Error
>Error : ErrorConstructor
x = x;
>x = x : unknown
>x : unknown
>x : unknown
x = pAny;
>x = pAny : any
>x : unknown
>pAny : any
x = pNever;
>x = pNever : never
>x : unknown
>pNever : never
x = pT;
>x = pT : T
>x : unknown
>pT : T
}
// unknown assignable only to itself and any
function f22(x: unknown) {
>f22 : (x: unknown) => void
>x : unknown
let v1: any = x;
>v1 : any
>x : unknown
let v2: unknown = x;
>v2 : unknown
>x : unknown
let v3: object = x; // Error
>v3 : object
>x : unknown
let v4: string = x; // Error
>v4 : string
>x : unknown
let v5: string[] = x; // Error
>v5 : string[]
>x : unknown
let v6: {} = x; // Error
>v6 : {}
>x : unknown
let v7: {} | null | undefined = x; // Error
>v7 : {} | null | undefined
>null : null
>x : unknown
}
// Type parameter 'T extends unknown' not related to object
function f23<T extends unknown>(x: T) {
>f23 : <T extends unknown>(x: T) => void
>T : T
>x : T
>T : T
let y: object = x; // Error
>y : object
>x : T
}
// Anything but primitive assignable to { [x: string]: unknown }
function f24(x: { [x: string]: unknown }) {
>f24 : (x: { [x: string]: unknown; }) => void
>x : { [x: string]: unknown; }
>x : string
x = {};
>x = {} : {}
>x : { [x: string]: unknown; }
>{} : {}
x = { a: 5 };
>x = { a: 5 } : { a: number; }
>x : { [x: string]: unknown; }
>{ a: 5 } : { a: number; }
>a : number
>5 : 5
x = [1, 2, 3];
>x = [1, 2, 3] : number[]
>x : { [x: string]: unknown; }
>[1, 2, 3] : number[]
>1 : 1
>2 : 2
>3 : 3
x = 123; // Error
>x = 123 : 123
>x : { [x: string]: unknown; }
>123 : 123
}
// Locals of type unknown always considered initialized
function f25() {
>f25 : () => void
let x: unknown;
>x : unknown
let y = x;
>y : unknown
>x : unknown
}
// Spread of unknown causes result to be unknown
function f26(x: {}, y: unknown, z: any) {
>f26 : (x: {}, y: unknown, z: any) => void
>x : {}
>y : unknown
>z : any
let o1 = { a: 42, ...x }; // { a: number }
>o1 : { a: number; }
>{ a: 42, ...x } : { a: number; }
>a : number
>42 : 42
>x : {}
let o2 = { a: 42, ...x, ...y }; // unknown
>o2 : unknown
>{ a: 42, ...x, ...y } : unknown
>a : number
>42 : 42
>x : {}
>y : unknown
let o3 = { a: 42, ...x, ...y, ...z }; // any
>o3 : any
>{ a: 42, ...x, ...y, ...z } : any
>a : number
>42 : 42
>x : {}
>y : unknown
>z : any
}
// Functions with unknown return type don't need return expressions
function f27(): unknown {
>f27 : () => unknown
}
// Rest type cannot be created from unknown
function f28(x: unknown) {
>f28 : (x: unknown) => void
>x : unknown
let { ...a } = x; // Error
>a : any
>x : unknown
}
// Class properties of type unknown don't need definite assignment
class C1 {
>C1 : C1
a: string; // Error
>a : string
b: unknown;
>b : unknown
c: any;
>c : any
}

View File

@ -0,0 +1,166 @@
// @strict: true
// In an intersection everything absorbs unknown
type T00 = unknown & null; // null
type T01 = unknown & undefined; // undefined
type T02 = unknown & null & undefined; // null & undefined (which becomes never in union)
type T03 = unknown & string; // string
type T04 = unknown & string[]; // string[]
type T05 = unknown & unknown; // unknown
type T06 = unknown & any; // any
// In a union an unknown absorbs everything
type T10 = unknown | null; // unknown
type T11 = unknown | undefined; // unknown
type T12 = unknown | null | undefined; // unknown
type T13 = unknown | string; // unknown
type T14 = unknown | string[]; // unknown
type T15 = unknown | unknown; // unknown
type T16 = unknown | any; // any
// Type variable and unknown in union and intersection
type T20<T> = T & {}; // T & {}
type T21<T> = T | {}; // T | {}
type T22<T> = T & unknown; // T
type T23<T> = T | unknown; // unknown
// unknown in conditional types
type T30<T> = unknown extends T ? true : false; // Deferred
type T31<T> = T extends unknown ? true : false; // Deferred (so it distributes)
type T32<T> = never extends T ? true : false; // true
type T33<T> = T extends never ? true : false; // Deferred
type T35<T> = T extends unknown ? { x: T } : false;
type T36 = T35<string | number>; // { x: string } | { x: number }
type T37 = T35<any>; // { x: any }
type T38 = T35<unknown>; // { x: unknown }
// keyof unknown
type T40 = keyof any; // string | number | symbol
type T41 = keyof unknown; // never
// Only equality operators are allowed with unknown
function f10(x: unknown) {
x == 5;
x !== 10;
x >= 0; // Error
x.foo; // Error
x[10]; // Error
x(); // Error
x + 1; // Error
x * 2; // Error
-x; // Error
+x; // Error
}
// No property accesses, element accesses, or function calls
function f11(x: unknown) {
x.foo; // Error
x[5]; // Error
x(); // Error
new x(); // Error
}
// typeof, instanceof, and user defined type predicates
declare function isFunction(x: unknown): x is Function;
function f20(x: unknown) {
if (typeof x === "string" || typeof x === "number") {
x; // string | number
}
if (x instanceof Error) {
x; // Error
}
if (isFunction(x)) {
x; // Function
}
}
// Homomorphic mapped type over unknown
type T50<T> = { [P in keyof T]: number };
type T51 = T50<any>; // { [x: string]: number }
type T52 = T50<unknown>; // {}
// Anything is assignable to unknown
function f21<T>(pAny: any, pNever: never, pT: T) {
let x: unknown;
x = 123;
x = "hello";
x = [1, 2, 3];
x = new Error();
x = x;
x = pAny;
x = pNever;
x = pT;
}
// unknown assignable only to itself and any
function f22(x: unknown) {
let v1: any = x;
let v2: unknown = x;
let v3: object = x; // Error
let v4: string = x; // Error
let v5: string[] = x; // Error
let v6: {} = x; // Error
let v7: {} | null | undefined = x; // Error
}
// Type parameter 'T extends unknown' not related to object
function f23<T extends unknown>(x: T) {
let y: object = x; // Error
}
// Anything but primitive assignable to { [x: string]: unknown }
function f24(x: { [x: string]: unknown }) {
x = {};
x = { a: 5 };
x = [1, 2, 3];
x = 123; // Error
}
// Locals of type unknown always considered initialized
function f25() {
let x: unknown;
let y = x;
}
// Spread of unknown causes result to be unknown
function f26(x: {}, y: unknown, z: any) {
let o1 = { a: 42, ...x }; // { a: number }
let o2 = { a: 42, ...x, ...y }; // unknown
let o3 = { a: 42, ...x, ...y, ...z }; // any
}
// Functions with unknown return type don't need return expressions
function f27(): unknown {
}
// Rest type cannot be created from unknown
function f28(x: unknown) {
let { ...a } = x; // Error
}
// Class properties of type unknown don't need definite assignment
class C1 {
a: string; // Error
b: unknown;
c: any;
}