Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
  •  
  •  
  •  
413 changes: 403 additions & 10 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ export function binarySearchKey<T, U>(array: readonly T[], key: U, keySelector:
while (low <= high) {
const middle = low + ((high - low) >> 1);
const midKey = keySelector(array[middle], middle);
switch (keyComparer(midKey, key)) {
switch (Math.sign(keyComparer(midKey, key))) {
case Comparison.LessThan:
low = middle + 1;
break;
Expand Down Expand Up @@ -1967,9 +1967,11 @@ export function equateStringsCaseSensitive(a: string, b: string): boolean {
return equateValues(a, b);
}

function compareComparableValues(a: string | undefined, b: string | undefined): Comparison;
function compareComparableValues(a: number | undefined, b: number | undefined): Comparison;
function compareComparableValues(a: string | number | undefined, b: string | number | undefined) {
/** @internal */
export function compareComparableValues(a: string | undefined, b: string | undefined): Comparison;
/** @internal */
export function compareComparableValues(a: number | undefined, b: number | undefined): Comparison;
export function compareComparableValues(a: string | number | undefined, b: string | number | undefined) {
return a === b ? Comparison.EqualTo :
a === undefined ? Comparison.LessThan :
b === undefined ? Comparison.GreaterThan :
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/corePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export const versionMajorMinor = "6.0";
/** The version of the TypeScript compiler release */
export const version: string = `${versionMajorMinor}.0-dev`;

const tsgoCompatEnv = typeof process !== "undefined" && process.env ? process.env.TSGO_COMPAT : undefined;

/** @internal */
export const TSGO_COMPAT: boolean = tsgoCompatEnv ? tsgoCompatEnv === "true" : true;

/**
* Type of objects whose values are all of the same type.
* The `in` and `for-in` operators can *not* be safely used,
Expand Down
73 changes: 41 additions & 32 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6319,42 +6319,49 @@ export interface SerializedTypeEntry {
trackedSymbols: readonly TrackedSymbol[] | undefined;
}

// Note that for types of different kinds, the numeric values of TypeFlags determine the order
// computed by the CompareTypes function and therefore the order of constituent types in union types.
// Since union type processing often bails out early when a result is known, it is important to order
// TypeFlags in increasing order of potential type complexity. In particular, indexed access and
// conditional types should sort last as those types are potentially recursive and possibly infinite.

// dprint-ignore
export const enum TypeFlags {
Any = 1 << 0,
Unknown = 1 << 1,
String = 1 << 2,
Number = 1 << 3,
Boolean = 1 << 4,
Enum = 1 << 5, // Numeric computed enum member value
BigInt = 1 << 6,
StringLiteral = 1 << 7,
NumberLiteral = 1 << 8,
BooleanLiteral = 1 << 9,
EnumLiteral = 1 << 10, // Always combined with StringLiteral, NumberLiteral, or Union
BigIntLiteral = 1 << 11,
ESSymbol = 1 << 12, // Type of symbol primitive introduced in ES6
UniqueESSymbol = 1 << 13, // unique symbol
Void = 1 << 14,
Undefined = 1 << 15,
Null = 1 << 16,
Never = 1 << 17, // Never type
TypeParameter = 1 << 18, // Type parameter
Object = 1 << 19, // Object type
Union = 1 << 20, // Union (T | U)
Intersection = 1 << 21, // Intersection (T & U)
Index = 1 << 22, // keyof T
IndexedAccess = 1 << 23, // T[K]
Conditional = 1 << 24, // T extends U ? X : Y
Substitution = 1 << 25, // Type parameter substitution
NonPrimitive = 1 << 26, // intrinsic object type
TemplateLiteral = 1 << 27, // Template literal type
StringMapping = 1 << 28, // Uppercase/Lowercase type
Undefined = 1 << 2,
Null = 1 << 3,
Void = 1 << 4,
String = 1 << 5,
Number = 1 << 6,
BigInt = 1 << 7,
Boolean = 1 << 8,
ESSymbol = 1 << 9, // Type of symbol primitive introduced in ES6
StringLiteral = 1 << 10,
NumberLiteral = 1 << 11,
BigIntLiteral = 1 << 12,
BooleanLiteral = 1 << 13,
UniqueESSymbol = 1 << 14, // unique symbol
EnumLiteral = 1 << 15, // Always combined with StringLiteral, NumberLiteral, or Union
Enum = 1 << 16, // Numeric computed enum member value (must be right after EnumLiteral, see getSortOrderFlags)
NonPrimitive = 1 << 17, // intrinsic object type
Never = 1 << 18, // Never type
TypeParameter = 1 << 19, // Type parameter
Object = 1 << 20, // Object type
Index = 1 << 21, // keyof T
TemplateLiteral = 1 << 22, // Template literal type
StringMapping = 1 << 23, // Uppercase/Lowercase type
Substitution = 1 << 24, // Type parameter substitution
IndexedAccess = 1 << 25, // T[K]
Conditional = 1 << 26, // T extends U ? X : Y
Union = 1 << 27, // Union (T | U)
Intersection = 1 << 28, // Intersection (T & U)
/** @internal */
Reserved1 = 1 << 29, // Used by union/intersection type construction
/** @internal */
Reserved2 = 1 << 30, // Used by union/intersection type construction

/** @internal */
Reserved3 = 1 << 31,
/** @internal */
AnyOrUnknown = Any | Unknown,
/** @internal */
Expand Down Expand Up @@ -6538,14 +6545,16 @@ export const enum ObjectFlags {
PropagatingFlags = ContainsWideningType | ContainsObjectOrArrayLiteral | NonInferrableType,
/** @internal */
InstantiatedMapped = Mapped | Instantiated,
// Object flags that uniquely identify the kind of ObjectType
/** @internal */
ObjectTypeKindMask = ClassOrInterface | Reference | Tuple | Anonymous | Mapped | ReverseMapped | EvolvingArray,


// Flags that require TypeFlags.Object
ContainsSpread = 1 << 21, // Object literal contains spread operation
ObjectRestType = 1 << 22, // Originates in object rest declaration
InstantiationExpressionType = 1 << 23, // Originates in instantiation expression

// Object flags that uniquely identify the kind of ObjectType
/** @internal */
ObjectTypeKindMask = ClassOrInterface | Reference | Tuple | Anonymous | Mapped | ReverseMapped | EvolvingArray | InstantiationExpressionType | SingleSignatureType,

/** @internal */
IsClassInstanceClone = 1 << 24, // Type is a clone of a class instance type
// Flags that require TypeFlags.Object and ObjectFlags.Reference
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/TypeGuardWithArrayUnion.types
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class Message {
function saySize(message: Message | Message[]) {
>saySize : (message: Message | Message[]) => number
> : ^ ^^ ^^^^^^^^^^^
>message : Message | Message[]
>message : Message[] | Message
> : ^^^^^^^^^^^^^^^^^^^

if (message instanceof Array) {
>message instanceof Array : boolean
> : ^^^^^^^
>message : Message | Message[]
>message : Message[] | Message
> : ^^^^^^^^^^^^^^^^^^^
>Array : ArrayConstructor
> : ^^^^^^^^^^^^^^^^
Expand Down
22 changes: 11 additions & 11 deletions tests/baselines/reference/TypeGuardWithEnumUnion.types
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function f1(x: Color | string) {
if (typeof x === "number") {
>typeof x === "number" : boolean
> : ^^^^^^^
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>typeof x : "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>x : string | Color
> : ^^^^^^^^^^^^^^
Expand Down Expand Up @@ -53,15 +53,15 @@ function f1(x: Color | string) {
function f2(x: Color | string | string[]) {
>f2 : (x: Color | string | string[]) => void
> : ^ ^^ ^^^^^^^^^
>x : string | Color | string[]
>x : string | string[] | Color
> : ^^^^^^^^^^^^^^^^^^^^^^^^^

if (typeof x === "object") {
>typeof x === "object" : boolean
> : ^^^^^^^
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>typeof x : "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>x : string | Color | string[]
>x : string | string[] | Color
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
>"object" : "object"
> : ^^^^^^^^
Expand All @@ -79,9 +79,9 @@ function f2(x: Color | string | string[]) {
if (typeof x === "number") {
>typeof x === "number" : boolean
> : ^^^^^^^
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>typeof x : "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>x : string | Color | string[]
>x : string | string[] | Color
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
>"number" : "number"
> : ^^^^^^^^
Expand Down Expand Up @@ -110,9 +110,9 @@ function f2(x: Color | string | string[]) {
if (typeof x === "string") {
>typeof x === "string" : boolean
> : ^^^^^^^
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>typeof x : "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>x : string | Color | string[]
>x : string | string[] | Color
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
>"string" : "string"
> : ^^^^^^^^
Expand All @@ -129,13 +129,13 @@ function f2(x: Color | string | string[]) {
}
else {
var b = x;
>b : Color | string[]
>b : string[] | Color
> : ^^^^^^^^^^^^^^^^
>x : Color | string[]
>x : string[] | Color
> : ^^^^^^^^^^^^^^^^

var b: Color | string[];
>b : Color | string[]
>b : string[] | Color
> : ^^^^^^^^^^^^^^^^
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ambientExportDefaultErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare module "indirect" {
> : ^^^^^^^^^^^^^^^^^^^^^^^^^

export default typeof Foo.default;
>typeof Foo.default : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>typeof Foo.default : "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>Foo.default : number
> : ^^^^^^
Expand All @@ -58,7 +58,7 @@ declare module "indirect2" {
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^

export = typeof Foo2;
>typeof Foo2 : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>typeof Foo2 : "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>Foo2 : number
> : ^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/anonymousClassExpression1.types
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function f() {
return typeof class {} === "function";
>typeof class {} === "function" : boolean
> : ^^^^^^^
>typeof class {} : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>typeof class {} : "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>class {} : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
94 changes: 47 additions & 47 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6591,53 +6591,53 @@ declare namespace ts {
enum TypeFlags {
Any = 1,
Unknown = 2,
String = 4,
Number = 8,
Boolean = 16,
Enum = 32,
BigInt = 64,
StringLiteral = 128,
NumberLiteral = 256,
BooleanLiteral = 512,
EnumLiteral = 1024,
BigIntLiteral = 2048,
ESSymbol = 4096,
UniqueESSymbol = 8192,
Void = 16384,
Undefined = 32768,
Null = 65536,
Never = 131072,
TypeParameter = 262144,
Object = 524288,
Union = 1048576,
Intersection = 2097152,
Index = 4194304,
IndexedAccess = 8388608,
Conditional = 16777216,
Substitution = 33554432,
NonPrimitive = 67108864,
TemplateLiteral = 134217728,
StringMapping = 268435456,
Literal = 2944,
Unit = 109472,
Freshable = 2976,
StringOrNumberLiteral = 384,
PossiblyFalsy = 117724,
StringLike = 402653316,
NumberLike = 296,
BigIntLike = 2112,
BooleanLike = 528,
EnumLike = 1056,
ESSymbolLike = 12288,
VoidLike = 49152,
UnionOrIntersection = 3145728,
StructuredType = 3670016,
TypeVariable = 8650752,
InstantiableNonPrimitive = 58982400,
InstantiablePrimitive = 406847488,
Instantiable = 465829888,
StructuredOrInstantiable = 469499904,
Narrowable = 536624127,
Undefined = 4,
Null = 8,
Void = 16,
String = 32,
Number = 64,
BigInt = 128,
Boolean = 256,
ESSymbol = 512,
StringLiteral = 1024,
NumberLiteral = 2048,
BigIntLiteral = 4096,
BooleanLiteral = 8192,
UniqueESSymbol = 16384,
EnumLiteral = 32768,
Enum = 65536,
NonPrimitive = 131072,
Never = 262144,
TypeParameter = 524288,
Object = 1048576,
Index = 2097152,
TemplateLiteral = 4194304,
StringMapping = 8388608,
Substitution = 16777216,
IndexedAccess = 33554432,
Conditional = 67108864,
Union = 134217728,
Intersection = 268435456,
Literal = 15360,
Unit = 97292,
Freshable = 80896,
StringOrNumberLiteral = 3072,
PossiblyFalsy = 15868,
StringLike = 12583968,
NumberLike = 67648,
BigIntLike = 4224,
BooleanLike = 8448,
EnumLike = 98304,
ESSymbolLike = 16896,
VoidLike = 20,
UnionOrIntersection = 402653184,
StructuredType = 403701760,
TypeVariable = 34078720,
InstantiableNonPrimitive = 117964800,
InstantiablePrimitive = 14680064,
Instantiable = 132644864,
StructuredOrInstantiable = 536346624,
Narrowable = 536575971,
}
type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;
interface Type {
Expand Down
20 changes: 12 additions & 8 deletions tests/baselines/reference/arityAndOrderCompatibility01.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ arityAndOrderCompatibility01.ts(23,5): error TS2322: Type '{ 0: string; 1: numbe
arityAndOrderCompatibility01.ts(24,5): error TS2322: Type '[string, number]' is not assignable to type '[number]'.
Source has 2 element(s) but target allows only 1.
arityAndOrderCompatibility01.ts(25,5): error TS2322: Type 'StrNum' is not assignable to type '[number]'.
Types of property '0' are incompatible.
Type 'string' is not assignable to type 'number'.
The types returned by 'pop()' are incompatible between these types.
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
arityAndOrderCompatibility01.ts(26,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number]'.
arityAndOrderCompatibility01.ts(27,5): error TS2322: Type '[string, number]' is not assignable to type '[string]'.
Source has 2 element(s) but target allows only 1.
arityAndOrderCompatibility01.ts(28,5): error TS2322: Type 'StrNum' is not assignable to type '[string]'.
Types of property 'length' are incompatible.
Type '2' is not assignable to type '1'.
The types returned by 'pop()' are incompatible between these types.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
arityAndOrderCompatibility01.ts(29,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string]'.
arityAndOrderCompatibility01.ts(30,5): error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
Type at position 0 in source is not compatible with type at position 0 in target.
Expand Down Expand Up @@ -78,8 +80,9 @@ arityAndOrderCompatibility01.ts(32,5): error TS2322: Type '{ 0: string; 1: numbe
var l2: [number] = y;
~~
!!! error TS2322: Type 'StrNum' is not assignable to type '[number]'.
!!! error TS2322: Types of property '0' are incompatible.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
!!! error TS2322: The types returned by 'pop()' are incompatible between these types.
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
var l3: [number] = z;
~~
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number]'.
Expand All @@ -90,8 +93,9 @@ arityAndOrderCompatibility01.ts(32,5): error TS2322: Type '{ 0: string; 1: numbe
var m2: [string] = y;
~~
!!! error TS2322: Type 'StrNum' is not assignable to type '[string]'.
!!! error TS2322: Types of property 'length' are incompatible.
!!! error TS2322: Type '2' is not assignable to type '1'.
!!! error TS2322: The types returned by 'pop()' are incompatible between these types.
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
var m3: [string] = z;
~~
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string]'.
Expand Down
Loading
Loading