diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8fd13d3d7b8d7..199fe14a030ae 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -265,14 +265,24 @@ namespace ts { Uppercase, Lowercase, Capitalize, - Uncapitalize + Uncapitalize, + Integer, + Add, + Subtract, + Multiply, + Divide, } const intrinsicTypeKinds: ReadonlyESMap = new Map(getEntries({ Uppercase: IntrinsicTypeKind.Uppercase, Lowercase: IntrinsicTypeKind.Lowercase, Capitalize: IntrinsicTypeKind.Capitalize, - Uncapitalize: IntrinsicTypeKind.Uncapitalize + Uncapitalize: IntrinsicTypeKind.Uncapitalize, + Integer: IntrinsicTypeKind.Integer, + Add: IntrinsicTypeKind.Add, + Subtract: IntrinsicTypeKind.Subtract, + Multiply: IntrinsicTypeKind.Multiply, + Divide: IntrinsicTypeKind.Divide })); function SymbolLinks(this: SymbolLinks) { @@ -757,6 +767,7 @@ namespace ts { const indexedAccessTypes = new Map(); const templateLiteralTypes = new Map(); const stringMappingTypes = new Map(); + const calculationTypes = new Map(); const substitutionTypes = new Map(); const subtypeReductionCache = new Map(); const evolvingArrayTypes: EvolvingArrayType[] = []; @@ -5032,6 +5043,9 @@ namespace ts { const typeNode = typeToTypeNodeHelper((type as StringMappingType).type, context); return symbolToTypeNode((type as StringMappingType).symbol, context, SymbolFlags.Type, [typeNode]); } + if (type.flags & TypeFlags.Calculation) { + return symbolToTypeNode((type as CalculationType).symbol, context, SymbolFlags.Type, (type as CalculationType).types.map(type => typeToTypeNodeHelper(type, context))); + } if (type.flags & TypeFlags.IndexedAccess) { const objectTypeNode = typeToTypeNodeHelper((type as IndexedAccessType).objectType, context); const indexTypeNode = typeToTypeNodeHelper((type as IndexedAccessType).indexType, context); @@ -11991,7 +12005,7 @@ namespace ts { } function getBaseConstraintOfType(type: Type): Type | undefined { - if (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.UnionOrIntersection | TypeFlags.TemplateLiteral | TypeFlags.StringMapping)) { + if (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.UnionOrIntersection | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.Calculation)) { const constraint = getResolvedBaseConstraint(type as InstantiableType | UnionOrIntersectionType); return constraint !== noConstraintType && constraint !== circularConstraintType ? constraint : undefined; } @@ -12101,7 +12115,16 @@ namespace ts { } if (t.flags & TypeFlags.StringMapping) { const constraint = getBaseConstraint((t as StringMappingType).type); - return constraint ? getStringMappingType((t as StringMappingType).symbol, constraint) : stringType; + return constraint ? getIntrinsicMappingType((t as StringMappingType).symbol, constraint) : stringType; + } + if (t.flags & TypeFlags.Calculation) { + const types = (t as CalculationType).types; + const constraints = mapDefined(types, getBaseConstraint); + return constraints.length === types.length + ? constraints.length === 1 + ? getIntrinsicMappingType((t as CalculationType).symbol, constraints[0]) + : getIntrinsicMappingType2((t as CalculationType).symbol, constraints[0], constraints[1]) + : numberType; } if (t.flags & TypeFlags.IndexedAccess) { if (isMappedTypeGenericIndexedAccess(t)) { @@ -13464,8 +13487,13 @@ namespace ts { function getTypeAliasInstantiation(symbol: Symbol, typeArguments: readonly Type[] | undefined, aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[]): Type { const type = getDeclaredTypeOfSymbol(symbol); - if (type === intrinsicMarkerType && intrinsicTypeKinds.has(symbol.escapedName as string) && typeArguments && typeArguments.length === 1) { - return getStringMappingType(symbol, typeArguments[0]); + if (type === intrinsicMarkerType && intrinsicTypeKinds.has(symbol.escapedName as string) && typeArguments) { + if (typeArguments.length === 1) { + return getIntrinsicMappingType(symbol, typeArguments[0]); + } + if (typeArguments.length === 2) { + return getIntrinsicMappingType2(symbol, typeArguments[0], typeArguments[1]); + } } const links = getSymbolLinks(symbol); const typeParameters = links.typeParameters!; @@ -14466,7 +14494,7 @@ namespace ts { const flags = t.flags; const remove = flags & (TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && includes & TypeFlags.String || - flags & TypeFlags.NumberLiteral && includes & TypeFlags.Number || + flags & (TypeFlags.NumberLiteral | TypeFlags.Calculation) && includes & TypeFlags.Number || flags & TypeFlags.BigIntLiteral && includes & TypeFlags.BigInt || flags & TypeFlags.UniqueESSymbol && includes & TypeFlags.ESSymbol || reduceVoidUndefined && flags & TypeFlags.Undefined && includes & TypeFlags.Void || @@ -15066,6 +15094,7 @@ namespace ts { type.flags & TypeFlags.IndexedAccess ? isDistributive((type as IndexedAccessType).objectType) && isDistributive((type as IndexedAccessType).indexType) : type.flags & TypeFlags.Substitution ? isDistributive((type as SubstitutionType).substitute) : type.flags & TypeFlags.StringMapping ? isDistributive((type as StringMappingType).type) : + type.flags & TypeFlags.Calculation ? isDistributive((type as CalculationType).types[0]) || ((type as CalculationType).types.length === 2 && isDistributive((type as CalculationType).types[1]!)) : false; } } @@ -15234,13 +15263,29 @@ namespace ts { return type; } - function getStringMappingType(symbol: Symbol, type: Type): Type { - return type.flags & (TypeFlags.Union | TypeFlags.Never) ? mapType(type, t => getStringMappingType(symbol, t)) : - isGenericIndexType(type) ? getStringMappingTypeForGenericType(symbol, type) : + function getIntrinsicMappingType(symbol: Symbol, type: Type): Type { + return type.flags & (TypeFlags.Union | TypeFlags.Never) ? mapType(type, t => getIntrinsicMappingType(symbol, t)) : + isGenericIndexType(type) ? + (getBaseConstraintOfType(type)?.flags ?? 0) & TypeFlags.StringLike ? getStringMappingTypeForGenericType(symbol, type) : + getCalculationTypeForGenericType(symbol, [type]) : type.flags & TypeFlags.StringLiteral ? getStringLiteralType(applyStringMapping(symbol, (type as StringLiteralType).value)) : + type.flags & TypeFlags.NumberLiteral ? applyNumberMapping(symbol, type as NumberLiteralType) : type; } + // Currently the only 2-type intrinsics are Calculation types (i.e. numeric) + function getIntrinsicMappingType2(symbol: Symbol, type1: Type, type2: Type): Type { + return type1.flags & (TypeFlags.Union | TypeFlags.Never) ? mapType(type1, t => getIntrinsicMappingType2(symbol, t, type2)) : + isGenericIndexType(type1) || isGenericIndexType(type2) ? getCalculationTypeForGenericType(symbol, [type1, type2]) : + type2.flags & (TypeFlags.Union | TypeFlags.Never) ? mapType(type2, t => getIntrinsicMappingType2(symbol, type1, t)) : + // handle division by zero + intrinsicTypeKinds.get(symbol.escapedName as string) === IntrinsicTypeKind.Divide && type2.flags & TypeFlags.NumberLiteral && (type2 as NumberLiteralType).value === 0 ? neverType : + type1.flags & TypeFlags.NumberLiteral ? + type2.flags & TypeFlags.NumberLiteral ? applyNumberMapping2(symbol, type1 as NumberLiteralType, type2 as NumberLiteralType) : + type2 : + type1; + } + function applyStringMapping(symbol: Symbol, str: string) { switch (intrinsicTypeKinds.get(symbol.escapedName as string)) { case IntrinsicTypeKind.Uppercase: return str.toUpperCase(); @@ -15251,6 +15296,23 @@ namespace ts { return str; } + function applyNumberMapping(symbol: Symbol, n: NumberLiteralType): Type { + switch (intrinsicTypeKinds.get(symbol.escapedName as string)) { + case IntrinsicTypeKind.Integer: return getNumberLiteralType(Math.floor(n.value)); + } + return n; + } + + function applyNumberMapping2(symbol: Symbol, a: NumberLiteralType, b: NumberLiteralType): Type { + switch (intrinsicTypeKinds.get(symbol.escapedName as string)) { + case IntrinsicTypeKind.Add: return getNumberLiteralType(a.value + b.value); + case IntrinsicTypeKind.Subtract: return getNumberLiteralType(a.value - b.value); + case IntrinsicTypeKind.Multiply: return getNumberLiteralType(a.value * b.value); + case IntrinsicTypeKind.Divide: return getNumberLiteralType(a.value / b.value); + } + return a; + } + function getStringMappingTypeForGenericType(symbol: Symbol, type: Type): Type { const id = `${getSymbolId(symbol)},${getTypeId(type)}`; let result = stringMappingTypes.get(id); @@ -15267,6 +15329,22 @@ namespace ts { return result; } + function getCalculationTypeForGenericType(symbol: Symbol, types: [Type] | [Type, Type]): Type { + const id = `${getSymbolId(symbol)},${types.map(getTypeId).join(",")}`; + let result = calculationTypes.get(id); + if (!result) { + calculationTypes.set(id, result = createCalculationType(symbol, types)); + } + return result; + } + + function createCalculationType(symbol: Symbol, types: [Type] | [Type, Type]) { + const result = createType(TypeFlags.Calculation) as CalculationType; + result.symbol = symbol; + result.types = types; + return result; + } + function createIndexedAccessType(objectType: Type, indexType: Type, accessFlags: AccessFlags, aliasSymbol: Symbol | undefined, aliasTypeArguments: readonly Type[] | undefined) { const type = createType(TypeFlags.IndexedAccess) as IndexedAccessType; type.objectType = objectType; @@ -15542,7 +15620,7 @@ namespace ts { return (type as SubstitutionType).objectFlags & ObjectFlags.IsGenericType; } return (type.flags & TypeFlags.InstantiableNonPrimitive || isGenericMappedType(type) || isGenericTupleType(type) ? ObjectFlags.IsGenericObjectType : 0) | - (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0); + (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.Calculation) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0); } function getSimplifiedType(type: Type, writing: boolean): Type { @@ -17006,7 +17084,14 @@ namespace ts { return getTemplateLiteralType((type as TemplateLiteralType).texts, instantiateTypes((type as TemplateLiteralType).types, mapper)); } if (flags & TypeFlags.StringMapping) { - return getStringMappingType((type as StringMappingType).symbol, instantiateType((type as StringMappingType).type, mapper)); + return getIntrinsicMappingType((type as StringMappingType).symbol, instantiateType((type as StringMappingType).type, mapper)); + } + if (flags & TypeFlags.Calculation) { + const calculation = type as CalculationType; + if (calculation.types.length === 1) { + return getIntrinsicMappingType(calculation.symbol, instantiateType(calculation.types[0], mapper)); + } + return getIntrinsicMappingType2(calculation.symbol, instantiateType(calculation.types[0], mapper), instantiateType(calculation.types[1], mapper)); } if (flags & TypeFlags.IndexedAccess) { const newAliasSymbol = aliasSymbol || type.aliasSymbol; @@ -19415,6 +19500,27 @@ namespace ts { } } } + else if (sourceFlags & TypeFlags.Calculation) { + const s = source as CalculationType; + if (targetFlags & TypeFlags.Calculation && s.symbol === (target as CalculationType).symbol) { + const t = target as CalculationType; + if (result = isRelatedTo(s.types[0], t.types[0], RecursionFlags.Both, reportErrors)) { + resetErrorInfo(saveErrorInfo); + return result; + } + if (s.types.length === 2 && t.types.length === 2 && (result = isRelatedTo(s.types[1], t.types[1], RecursionFlags.Both, reportErrors))) { + resetErrorInfo(saveErrorInfo); + return result; + } + } + else { + const constraint = getBaseConstraintOfType(source); + if (constraint && (result = isRelatedTo(constraint, target, RecursionFlags.Source, reportErrors))) { + resetErrorInfo(saveErrorInfo); + return result; + } + } + } else if (sourceFlags & TypeFlags.StringMapping) { if (targetFlags & TypeFlags.StringMapping && (source as StringMappingType).symbol === (target as StringMappingType).symbol) { if (result = isRelatedTo((source as StringMappingType).type, (target as StringMappingType).type, RecursionFlags.Both, reportErrors)) { @@ -21005,7 +21111,7 @@ namespace ts { function getBaseTypeOfLiteralType(type: Type): Type { return type.flags & TypeFlags.EnumLiteral ? getBaseTypeOfEnumLiteralType(type as LiteralType) : type.flags & (TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) ? stringType : - type.flags & TypeFlags.NumberLiteral ? numberType : + type.flags & (TypeFlags.NumberLiteral | TypeFlags.Calculation) ? numberType : type.flags & TypeFlags.BigIntLiteral ? bigintType : type.flags & TypeFlags.BooleanLiteral ? booleanType : type.flags & TypeFlags.Union ? mapType(type as UnionType, getBaseTypeOfLiteralType) : @@ -22152,6 +22258,16 @@ namespace ts { inferFromTypes((source as StringMappingType).type, (target as StringMappingType).type); } } + else if (source.flags & TypeFlags.Calculation && target.flags & TypeFlags.Calculation) { + const sourceCalculation = source as CalculationType; + const targetCalculation = target as CalculationType; + if (sourceCalculation.symbol === targetCalculation.symbol) { + inferFromTypes(sourceCalculation.types[0], targetCalculation.types[0]); + if (sourceCalculation.types.length === 2 && targetCalculation.types.length === 2) { + inferFromTypes(sourceCalculation.types[1], targetCalculation.types[1]); + } + } + } else if (source.flags & TypeFlags.Substitution) { inferFromTypes((source as SubstitutionType).baseType, target); const oldPriority = priority; @@ -22635,7 +22751,7 @@ namespace ts { function hasPrimitiveConstraint(type: TypeParameter): boolean { const constraint = getConstraintOfTypeParameter(type); - return !!constraint && maybeTypeOfKind(constraint.flags & TypeFlags.Conditional ? getDefaultConstraintOfConditionalType(constraint as ConditionalType) : constraint, TypeFlags.Primitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping); + return !!constraint && maybeTypeOfKind(constraint.flags & TypeFlags.Conditional ? getDefaultConstraintOfConditionalType(constraint as ConditionalType) : constraint, TypeFlags.Primitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.Calculation); } function isObjectLiteralType(type: Type) { @@ -23561,11 +23677,11 @@ namespace ts { // types we don't actually care about. function replacePrimitivesWithLiterals(typeWithPrimitives: Type, typeWithLiterals: Type) { if (maybeTypeOfKind(typeWithPrimitives, TypeFlags.String | TypeFlags.TemplateLiteral | TypeFlags.Number | TypeFlags.BigInt) && - maybeTypeOfKind(typeWithLiterals, TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.NumberLiteral | TypeFlags.BigIntLiteral)) { + maybeTypeOfKind(typeWithLiterals, TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.Calculation | TypeFlags.NumberLiteral | TypeFlags.BigIntLiteral)) { return mapType(typeWithPrimitives, t => t.flags & TypeFlags.String ? extractTypesOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) : isPatternLiteralType(t) && !maybeTypeOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) ? extractTypesOfKind(typeWithLiterals, TypeFlags.StringLiteral) : - t.flags & TypeFlags.Number ? extractTypesOfKind(typeWithLiterals, TypeFlags.Number | TypeFlags.NumberLiteral) : + t.flags & TypeFlags.Number ? extractTypesOfKind(typeWithLiterals, TypeFlags.Number | TypeFlags.NumberLiteral | TypeFlags.Calculation) : t.flags & TypeFlags.BigInt ? extractTypesOfKind(typeWithLiterals, TypeFlags.BigInt | TypeFlags.BigIntLiteral) : t); } return typeWithPrimitives; @@ -29710,7 +29826,7 @@ namespace ts { else { const contextualType = getIndexedAccessType(restType, getNumberLiteralType(i - index), AccessFlags.Contextual); const argType = checkExpressionWithContextualType(arg, contextualType, context, checkMode); - const hasPrimitiveContextualType = maybeTypeOfKind(contextualType, TypeFlags.Primitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping); + const hasPrimitiveContextualType = maybeTypeOfKind(contextualType, TypeFlags.Primitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.Calculation); types.push(hasPrimitiveContextualType ? getRegularTypeOfLiteralType(argType) : getWidenedLiteralType(argType)); flags.push(ElementFlags.Required); } @@ -34114,7 +34230,7 @@ namespace ts { // If the contextual type is a literal of a particular primitive type, we consider this a // literal context for all literals of that primitive type. return !!(contextualType.flags & (TypeFlags.StringLiteral | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && maybeTypeOfKind(candidateType, TypeFlags.StringLiteral) || - contextualType.flags & TypeFlags.NumberLiteral && maybeTypeOfKind(candidateType, TypeFlags.NumberLiteral) || + contextualType.flags & (TypeFlags.NumberLiteral | TypeFlags.Calculation) && maybeTypeOfKind(candidateType, TypeFlags.NumberLiteral) || contextualType.flags & TypeFlags.BigIntLiteral && maybeTypeOfKind(candidateType, TypeFlags.BigIntLiteral) || contextualType.flags & TypeFlags.BooleanLiteral && maybeTypeOfKind(candidateType, TypeFlags.BooleanLiteral) || contextualType.flags & TypeFlags.UniqueESSymbol && maybeTypeOfKind(candidateType, TypeFlags.UniqueESSymbol)); @@ -39698,7 +39814,7 @@ namespace ts { checkExportsOnMergedDeclarations(node); checkTypeParameters(node.typeParameters); if (node.type.kind === SyntaxKind.IntrinsicKeyword) { - if (!intrinsicTypeKinds.has(node.name.escapedText as string) || length(node.typeParameters) !== 1) { + if (!intrinsicTypeKinds.has(node.name.escapedText as string) || !(length(node.typeParameters) === 1 || length(node.typeParameters) === 2)) { error(node.type, Diagnostics.The_intrinsic_keyword_can_only_be_used_to_declare_compiler_provided_intrinsic_types); } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index dc31b5c79cf2f..9d0c41e44db75 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5160,6 +5160,7 @@ namespace ts { NonPrimitive = 1 << 26, // intrinsic object type TemplateLiteral = 1 << 27, // Template literal type StringMapping = 1 << 28, // Uppercase/Lowercase type + Calculation = 1 << 29, // Math type /* @internal */ AnyOrUnknown = Any | Unknown, @@ -5178,7 +5179,7 @@ namespace ts { /* @internal */ Primitive = String | Number | BigInt | Boolean | Enum | EnumLiteral | ESSymbol | Void | Undefined | Null | Literal | UniqueESSymbol, StringLike = String | StringLiteral | TemplateLiteral | StringMapping, - NumberLike = Number | NumberLiteral | Enum, + NumberLike = Number | NumberLiteral | Enum | Calculation, BigIntLike = BigInt | BigIntLiteral, BooleanLike = Boolean | BooleanLiteral, EnumLike = Enum | EnumLiteral, @@ -5192,7 +5193,7 @@ namespace ts { StructuredType = Object | Union | Intersection, TypeVariable = TypeParameter | IndexedAccess, InstantiableNonPrimitive = TypeVariable | Conditional | Substitution, - InstantiablePrimitive = Index | TemplateLiteral | StringMapping, + InstantiablePrimitive = Index | TemplateLiteral | StringMapping | Calculation, Instantiable = InstantiableNonPrimitive | InstantiablePrimitive, StructuredOrInstantiable = StructuredType | Instantiable, /* @internal */ @@ -5716,6 +5717,11 @@ namespace ts { type: Type; } + export interface CalculationType extends InstantiableType { + symbol: Symbol; + types: [Type] | [Type, Type]; + } + // Type parameter substitution (TypeFlags.Substitution) // Substitution types are created for type parameters or indexed access types that occur in the // true branch of a conditional type. For example, in 'T extends string ? Foo : Bar', the diff --git a/src/harness/fourslashInterfaceImpl.ts b/src/harness/fourslashInterfaceImpl.ts index 98b61cf2fb55a..9aed4139b4ee0 100644 --- a/src/harness/fourslashInterfaceImpl.ts +++ b/src/harness/fourslashInterfaceImpl.ts @@ -1144,6 +1144,11 @@ namespace FourSlashInterface { typeEntry("Lowercase"), typeEntry("Capitalize"), typeEntry("Uncapitalize"), + typeEntry("Integer"), + typeEntry("Add"), + typeEntry("Subtract"), + typeEntry("Multiply"), + typeEntry("Divide"), interfaceEntry("ThisType"), varEntry("ArrayBuffer"), interfaceEntry("ArrayBufferTypes"), diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index b0dc84de6b9f4..ea690215444b4 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -1606,6 +1606,31 @@ type Capitalize = intrinsic; */ type Uncapitalize = intrinsic; +/** + * Convert number literal type to integer + */ +type Integer = intrinsic; + +/** + * Add two literal numbers + */ +type Add = intrinsic; + +/** + * Subtract two literal numbers + */ +type Subtract = intrinsic; + +/** + * Multiply two literal numbers + */ +type Multiply = intrinsic; + +/** + * Divide two literal numbers + */ +type Divide = intrinsic; + /** * Marker for contextual 'this' type */ diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 2cc99e632b4b5..9d47373beb7b4 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2607,12 +2607,13 @@ declare namespace ts { NonPrimitive = 67108864, TemplateLiteral = 134217728, StringMapping = 268435456, + Calculation = 536870912, Literal = 2944, Unit = 109440, StringOrNumberLiteral = 384, PossiblyFalsy = 117724, StringLike = 402653316, - NumberLike = 296, + NumberLike = 536871208, BigIntLike = 2112, BooleanLike = 528, EnumLike = 1056, @@ -2622,10 +2623,10 @@ declare namespace ts { StructuredType = 3670016, TypeVariable = 8650752, InstantiableNonPrimitive = 58982400, - InstantiablePrimitive = 406847488, - Instantiable = 465829888, - StructuredOrInstantiable = 469499904, - Narrowable = 536624127, + InstantiablePrimitive = 943718400, + Instantiable = 1002700800, + StructuredOrInstantiable = 1006370816, + Narrowable = 1073495039, } export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression; export interface Type { @@ -2787,6 +2788,10 @@ declare namespace ts { symbol: Symbol; type: Type; } + export interface CalculationType extends InstantiableType { + symbol: Symbol; + types: [Type] | [Type, Type]; + } export interface SubstitutionType extends InstantiableType { objectFlags: ObjectFlags; baseType: Type; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 588f4a2d550f7..4a51c5e6cd2c9 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2607,12 +2607,13 @@ declare namespace ts { NonPrimitive = 67108864, TemplateLiteral = 134217728, StringMapping = 268435456, + Calculation = 536870912, Literal = 2944, Unit = 109440, StringOrNumberLiteral = 384, PossiblyFalsy = 117724, StringLike = 402653316, - NumberLike = 296, + NumberLike = 536871208, BigIntLike = 2112, BooleanLike = 528, EnumLike = 1056, @@ -2622,10 +2623,10 @@ declare namespace ts { StructuredType = 3670016, TypeVariable = 8650752, InstantiableNonPrimitive = 58982400, - InstantiablePrimitive = 406847488, - Instantiable = 465829888, - StructuredOrInstantiable = 469499904, - Narrowable = 536624127, + InstantiablePrimitive = 943718400, + Instantiable = 1002700800, + StructuredOrInstantiable = 1006370816, + Narrowable = 1073495039, } export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression; export interface Type { @@ -2787,6 +2788,10 @@ declare namespace ts { symbol: Symbol; type: Type; } + export interface CalculationType extends InstantiableType { + symbol: Symbol; + types: [Type] | [Type, Type]; + } export interface SubstitutionType extends InstantiableType { objectFlags: ObjectFlags; baseType: Type; diff --git a/tests/baselines/reference/intrinsicTypes.errors.txt b/tests/baselines/reference/intrinsicTypes.errors.txt index 1be876189490b..29140d7c66c1e 100644 --- a/tests/baselines/reference/intrinsicTypes.errors.txt +++ b/tests/baselines/reference/intrinsicTypes.errors.txt @@ -8,9 +8,28 @@ tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(42,5): error TS2322: tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(43,5): error TS2322: Type 'Uppercase' is not assignable to type 'Uppercase'. Type 'T' is not assignable to type 'U'. 'T' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint 'string'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(61,20): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(76,17): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(77,20): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(78,18): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(93,22): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(94,25): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(95,23): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(110,22): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(111,25): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(112,23): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(127,20): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(128,23): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(129,21): error TS2344: Type 'string' does not satisfy the constraint 'number'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(147,5): error TS2322: Type 'number' is not assignable to type 'Add'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(148,5): error TS2322: Type 'Multiply' is not assignable to type 'Add'. + Type 'number' is not assignable to type 'Add'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(149,5): error TS2322: Type 'number' is not assignable to type 'Multiply'. +tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(150,5): error TS2322: Type 'Add' is not assignable to type 'Multiply'. + Type 'number' is not assignable to type 'Multiply'. -==== tests/cases/conformance/types/typeAliases/intrinsicTypes.ts (8 errors) ==== +==== tests/cases/conformance/types/typeAliases/intrinsicTypes.ts (25 errors) ==== type TU1 = Uppercase<'hello'>; // "HELLO" type TU2 = Uppercase<'foo' | 'bar'>; // "FOO" | "BAR" type TU3 = Uppercase; // string @@ -83,4 +102,147 @@ tests/cases/conformance/types/typeAliases/intrinsicTypes.ts(43,5): error TS2322: function foo4(x: Uppercase) { return foo3(x); } + + type TI1 = Integer<3.5>; // 3 + type TI2 = Integer<2.5 | 3.4>; // 2 | 3 + type TI3 = Integer; // number + type TI4 = Integer; // any + type TI5 = Integer; // never + type TI6 = Integer<'42'>; // Error + ~~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + + type TA1 = Add<4, 2>; // 6 + type TA2L = Add<4 | 5, 2>; // 6 | 7 + type TA2R = Add<4, 2 | 3>; // 6 | 7 + type TA2LR = Add<4 | 5, 2 | 3>; // 6 | 7 | 8 + type TA3L = Add; // number + type TA3R = Add<4, number>; // number + type TA3LR = Add; // number + type TA4L = Add; // any + type TA4R = Add<4, any>; // any + type TA4LR = Add; // any + type TA5L = Add; // never + type TA5R = Add<4, never>; // never + type TA5LR = Add; // never + type TA6L = Add<'4', 2>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + type TA6R = Add<4, '2'>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + type TA6LR = Add<'4', '2'>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + + type TS1 = Subtract<4, 2>; // 2 + type TS2L = Subtract<4 | 5, 2>; // 2 | 3 + type TS2R = Subtract<4, 2 | 3>; // 2 | 1 + type TS2LR = Subtract<4 | 5, 2 | 3>; // 2 | 1 | 3 + type TS3L = Subtract; // number + type TS3R = Subtract<4, number>; // number + type TS3LR = Subtract; // number + type TS4L = Subtract; // any + type TS4R = Subtract<4, any>; // any + type TS4LR = Subtract; // any + type TS5L = Subtract; // never + type TS5R = Subtract<4, never>; // never + type TS5LR = Subtract; // never + type TS6L = Subtract<'4', 2>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + type TS6R = Subtract<4, '2'>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + type TS6LR = Subtract<'4', '2'>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + + type TM1 = Multiply<4, 2>; // 8 + type TM2L = Multiply<4 | 5, 2>; // 8 | 10 + type TM2R = Multiply<4, 2 | 3>; // 8 | 12 + type TM2LR = Multiply<4 | 5, 2 | 3>; // 8 | 12 | 10 | 15 + type TM3L = Multiply; // number + type TM3R = Multiply<4, number>; // number + type TM3LR = Multiply; // number + type TM4L = Multiply; // any + type TM4R = Multiply<4, any>; // any + type TM4LR = Multiply; // any + type TM5L = Multiply; // never + type TM5R = Multiply<4, never>; // never + type TM5LR = Multiply; // never + type TM6L = Multiply<'4', 2>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + type TM6R = Multiply<4, '2'>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + type TM6LR = Multiply<'4', '2'>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + + type TD1 = Divide<4, 2>; // 2 + type TD2L = Divide<4 | 5, 2>; // 2 | 2.5 + type TD2R = Divide<4, 2 | 4>; // 2 | 1 + type TD2LR = Divide<4 | 5, 2 | 4>; // 2 | 1 | 2.5 | 1.25 + type TD3L = Divide; // number + type TD3R = Divide<4, number>; // number + type TD3LR = Divide; // number + type TD4L = Divide; // any + type TD4R = Divide<4, any>; // any + type TD4LR = Divide; // any + type TD5L = Divide; // never + type TD5R = Divide<4, never>; // never + type TD5LR = Divide; // never + type TD6L = Divide<'4', 2>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + type TD6R = Divide<4, '2'>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + type TD6LR = Divide<'4', '2'>; // Error + ~~~ +!!! error TS2344: Type 'string' does not satisfy the constraint 'number'. + type TD7 = Divide<1, 0>; // never + + type TIX1 = Integer; + type TIX2 = TIX1<4.2>; // 4 + type TAX1 = Add; + type TAX2 = TAX1<4, 2>; // 6 + type TSX1 = Subtract; + type TSX2 = TSX1<4, 2>; // 6 + type TMX1 = Multiply; + type TMX2 = TMX1<4, 2>; // 8 + type TDX1 = Divide; + type TDX2 = TDX1<4, 2>; // 2 + type TAMX = Add<2, Multiply<5, 8>> // 42 + + function foo5(s: number, x: Add, y: Multiply) { + s = x; + s = y; + x = s; // Error + ~ +!!! error TS2322: Type 'number' is not assignable to type 'Add'. + x = y; // Error + ~ +!!! error TS2322: Type 'Multiply' is not assignable to type 'Add'. +!!! error TS2322: Type 'number' is not assignable to type 'Add'. + y = s; // Error + ~ +!!! error TS2322: Type 'number' is not assignable to type 'Multiply'. + y = x; // Error + ~ +!!! error TS2322: Type 'Add' is not assignable to type 'Multiply'. +!!! error TS2322: Type 'number' is not assignable to type 'Multiply'. + } + + function foo6(x: Add) { + let s: 3 | 4 = x; + } + + declare function foo7(x: Integer): T; + + function foo8(x: Integer) { + return foo7(x); + } \ No newline at end of file diff --git a/tests/baselines/reference/intrinsicTypes.js b/tests/baselines/reference/intrinsicTypes.js index 697371da226ff..6196cb924dd17 100644 --- a/tests/baselines/reference/intrinsicTypes.js +++ b/tests/baselines/reference/intrinsicTypes.js @@ -53,6 +53,113 @@ declare function foo3(x: Uppercase): T; function foo4(x: Uppercase) { return foo3(x); } + +type TI1 = Integer<3.5>; // 3 +type TI2 = Integer<2.5 | 3.4>; // 2 | 3 +type TI3 = Integer; // number +type TI4 = Integer; // any +type TI5 = Integer; // never +type TI6 = Integer<'42'>; // Error + +type TA1 = Add<4, 2>; // 6 +type TA2L = Add<4 | 5, 2>; // 6 | 7 +type TA2R = Add<4, 2 | 3>; // 6 | 7 +type TA2LR = Add<4 | 5, 2 | 3>; // 6 | 7 | 8 +type TA3L = Add; // number +type TA3R = Add<4, number>; // number +type TA3LR = Add; // number +type TA4L = Add; // any +type TA4R = Add<4, any>; // any +type TA4LR = Add; // any +type TA5L = Add; // never +type TA5R = Add<4, never>; // never +type TA5LR = Add; // never +type TA6L = Add<'4', 2>; // Error +type TA6R = Add<4, '2'>; // Error +type TA6LR = Add<'4', '2'>; // Error + +type TS1 = Subtract<4, 2>; // 2 +type TS2L = Subtract<4 | 5, 2>; // 2 | 3 +type TS2R = Subtract<4, 2 | 3>; // 2 | 1 +type TS2LR = Subtract<4 | 5, 2 | 3>; // 2 | 1 | 3 +type TS3L = Subtract; // number +type TS3R = Subtract<4, number>; // number +type TS3LR = Subtract; // number +type TS4L = Subtract; // any +type TS4R = Subtract<4, any>; // any +type TS4LR = Subtract; // any +type TS5L = Subtract; // never +type TS5R = Subtract<4, never>; // never +type TS5LR = Subtract; // never +type TS6L = Subtract<'4', 2>; // Error +type TS6R = Subtract<4, '2'>; // Error +type TS6LR = Subtract<'4', '2'>; // Error + +type TM1 = Multiply<4, 2>; // 8 +type TM2L = Multiply<4 | 5, 2>; // 8 | 10 +type TM2R = Multiply<4, 2 | 3>; // 8 | 12 +type TM2LR = Multiply<4 | 5, 2 | 3>; // 8 | 12 | 10 | 15 +type TM3L = Multiply; // number +type TM3R = Multiply<4, number>; // number +type TM3LR = Multiply; // number +type TM4L = Multiply; // any +type TM4R = Multiply<4, any>; // any +type TM4LR = Multiply; // any +type TM5L = Multiply; // never +type TM5R = Multiply<4, never>; // never +type TM5LR = Multiply; // never +type TM6L = Multiply<'4', 2>; // Error +type TM6R = Multiply<4, '2'>; // Error +type TM6LR = Multiply<'4', '2'>; // Error + +type TD1 = Divide<4, 2>; // 2 +type TD2L = Divide<4 | 5, 2>; // 2 | 2.5 +type TD2R = Divide<4, 2 | 4>; // 2 | 1 +type TD2LR = Divide<4 | 5, 2 | 4>; // 2 | 1 | 2.5 | 1.25 +type TD3L = Divide; // number +type TD3R = Divide<4, number>; // number +type TD3LR = Divide; // number +type TD4L = Divide; // any +type TD4R = Divide<4, any>; // any +type TD4LR = Divide; // any +type TD5L = Divide; // never +type TD5R = Divide<4, never>; // never +type TD5LR = Divide; // never +type TD6L = Divide<'4', 2>; // Error +type TD6R = Divide<4, '2'>; // Error +type TD6LR = Divide<'4', '2'>; // Error +type TD7 = Divide<1, 0>; // never + +type TIX1 = Integer; +type TIX2 = TIX1<4.2>; // 4 +type TAX1 = Add; +type TAX2 = TAX1<4, 2>; // 6 +type TSX1 = Subtract; +type TSX2 = TSX1<4, 2>; // 6 +type TMX1 = Multiply; +type TMX2 = TMX1<4, 2>; // 8 +type TDX1 = Divide; +type TDX2 = TDX1<4, 2>; // 2 +type TAMX = Add<2, Multiply<5, 8>> // 42 + +function foo5(s: number, x: Add, y: Multiply) { + s = x; + s = y; + x = s; // Error + x = y; // Error + y = s; // Error + y = x; // Error +} + +function foo6(x: Add) { + let s: 3 | 4 = x; +} + +declare function foo7(x: Integer): T; + +function foo8(x: Integer) { + return foo7(x); +} //// [intrinsicTypes.js] @@ -71,6 +178,20 @@ function foo2(x) { function foo4(x) { return foo3(x); } +function foo5(s, x, y) { + s = x; + s = y; + x = s; // Error + x = y; // Error + y = s; // Error + y = x; // Error +} +function foo6(x) { + var s = x; +} +function foo8(x) { + return foo7(x); +} //// [intrinsicTypes.d.ts] @@ -108,3 +229,89 @@ declare function foo1(s: string, x: Uppercase, declare function foo2(x: Uppercase): void; declare function foo3(x: Uppercase): T; declare function foo4(x: Uppercase): U; +declare type TI1 = Integer<3.5>; +declare type TI2 = Integer<2.5 | 3.4>; +declare type TI3 = Integer; +declare type TI4 = Integer; +declare type TI5 = Integer; +declare type TI6 = Integer<'42'>; +declare type TA1 = Add<4, 2>; +declare type TA2L = Add<4 | 5, 2>; +declare type TA2R = Add<4, 2 | 3>; +declare type TA2LR = Add<4 | 5, 2 | 3>; +declare type TA3L = Add; +declare type TA3R = Add<4, number>; +declare type TA3LR = Add; +declare type TA4L = Add; +declare type TA4R = Add<4, any>; +declare type TA4LR = Add; +declare type TA5L = Add; +declare type TA5R = Add<4, never>; +declare type TA5LR = Add; +declare type TA6L = Add<'4', 2>; +declare type TA6R = Add<4, '2'>; +declare type TA6LR = Add<'4', '2'>; +declare type TS1 = Subtract<4, 2>; +declare type TS2L = Subtract<4 | 5, 2>; +declare type TS2R = Subtract<4, 2 | 3>; +declare type TS2LR = Subtract<4 | 5, 2 | 3>; +declare type TS3L = Subtract; +declare type TS3R = Subtract<4, number>; +declare type TS3LR = Subtract; +declare type TS4L = Subtract; +declare type TS4R = Subtract<4, any>; +declare type TS4LR = Subtract; +declare type TS5L = Subtract; +declare type TS5R = Subtract<4, never>; +declare type TS5LR = Subtract; +declare type TS6L = Subtract<'4', 2>; +declare type TS6R = Subtract<4, '2'>; +declare type TS6LR = Subtract<'4', '2'>; +declare type TM1 = Multiply<4, 2>; +declare type TM2L = Multiply<4 | 5, 2>; +declare type TM2R = Multiply<4, 2 | 3>; +declare type TM2LR = Multiply<4 | 5, 2 | 3>; +declare type TM3L = Multiply; +declare type TM3R = Multiply<4, number>; +declare type TM3LR = Multiply; +declare type TM4L = Multiply; +declare type TM4R = Multiply<4, any>; +declare type TM4LR = Multiply; +declare type TM5L = Multiply; +declare type TM5R = Multiply<4, never>; +declare type TM5LR = Multiply; +declare type TM6L = Multiply<'4', 2>; +declare type TM6R = Multiply<4, '2'>; +declare type TM6LR = Multiply<'4', '2'>; +declare type TD1 = Divide<4, 2>; +declare type TD2L = Divide<4 | 5, 2>; +declare type TD2R = Divide<4, 2 | 4>; +declare type TD2LR = Divide<4 | 5, 2 | 4>; +declare type TD3L = Divide; +declare type TD3R = Divide<4, number>; +declare type TD3LR = Divide; +declare type TD4L = Divide; +declare type TD4R = Divide<4, any>; +declare type TD4LR = Divide; +declare type TD5L = Divide; +declare type TD5R = Divide<4, never>; +declare type TD5LR = Divide; +declare type TD6L = Divide<'4', 2>; +declare type TD6R = Divide<4, '2'>; +declare type TD6LR = Divide<'4', '2'>; +declare type TD7 = Divide<1, 0>; +declare type TIX1 = Integer; +declare type TIX2 = TIX1<4.2>; +declare type TAX1 = Add; +declare type TAX2 = TAX1<4, 2>; +declare type TSX1 = Subtract; +declare type TSX2 = TSX1<4, 2>; +declare type TMX1 = Multiply; +declare type TMX2 = TMX1<4, 2>; +declare type TDX1 = Divide; +declare type TDX2 = TDX1<4, 2>; +declare type TAMX = Add<2, Multiply<5, 8>>; +declare function foo5(s: number, x: Add, y: Multiply): void; +declare function foo6(x: Add): void; +declare function foo7(x: Integer): T; +declare function foo8(x: Integer): U; diff --git a/tests/baselines/reference/intrinsicTypes.symbols b/tests/baselines/reference/intrinsicTypes.symbols index 13efe08aaa237..3b43eb25efd5b 100644 --- a/tests/baselines/reference/intrinsicTypes.symbols +++ b/tests/baselines/reference/intrinsicTypes.symbols @@ -194,3 +194,422 @@ function foo4(x: Uppercase) { >x : Symbol(x, Decl(intrinsicTypes.ts, 51, 32)) } +type TI1 = Integer<3.5>; // 3 +>TI1 : Symbol(TI1, Decl(intrinsicTypes.ts, 53, 1)) +>Integer : Symbol(Integer, Decl(lib.es5.d.ts, --, --)) + +type TI2 = Integer<2.5 | 3.4>; // 2 | 3 +>TI2 : Symbol(TI2, Decl(intrinsicTypes.ts, 55, 24)) +>Integer : Symbol(Integer, Decl(lib.es5.d.ts, --, --)) + +type TI3 = Integer; // number +>TI3 : Symbol(TI3, Decl(intrinsicTypes.ts, 56, 30)) +>Integer : Symbol(Integer, Decl(lib.es5.d.ts, --, --)) + +type TI4 = Integer; // any +>TI4 : Symbol(TI4, Decl(intrinsicTypes.ts, 57, 27)) +>Integer : Symbol(Integer, Decl(lib.es5.d.ts, --, --)) + +type TI5 = Integer; // never +>TI5 : Symbol(TI5, Decl(intrinsicTypes.ts, 58, 24)) +>Integer : Symbol(Integer, Decl(lib.es5.d.ts, --, --)) + +type TI6 = Integer<'42'>; // Error +>TI6 : Symbol(TI6, Decl(intrinsicTypes.ts, 59, 26)) +>Integer : Symbol(Integer, Decl(lib.es5.d.ts, --, --)) + +type TA1 = Add<4, 2>; // 6 +>TA1 : Symbol(TA1, Decl(intrinsicTypes.ts, 60, 25)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA2L = Add<4 | 5, 2>; // 6 | 7 +>TA2L : Symbol(TA2L, Decl(intrinsicTypes.ts, 62, 21)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA2R = Add<4, 2 | 3>; // 6 | 7 +>TA2R : Symbol(TA2R, Decl(intrinsicTypes.ts, 63, 26)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA2LR = Add<4 | 5, 2 | 3>; // 6 | 7 | 8 +>TA2LR : Symbol(TA2LR, Decl(intrinsicTypes.ts, 64, 26)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA3L = Add; // number +>TA3L : Symbol(TA3L, Decl(intrinsicTypes.ts, 65, 31)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA3R = Add<4, number>; // number +>TA3R : Symbol(TA3R, Decl(intrinsicTypes.ts, 66, 27)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA3LR = Add; // number +>TA3LR : Symbol(TA3LR, Decl(intrinsicTypes.ts, 67, 27)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA4L = Add; // any +>TA4L : Symbol(TA4L, Decl(intrinsicTypes.ts, 68, 33)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA4R = Add<4, any>; // any +>TA4R : Symbol(TA4R, Decl(intrinsicTypes.ts, 69, 24)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA4LR = Add; // any +>TA4LR : Symbol(TA4LR, Decl(intrinsicTypes.ts, 70, 24)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA5L = Add; // never +>TA5L : Symbol(TA5L, Decl(intrinsicTypes.ts, 71, 27)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA5R = Add<4, never>; // never +>TA5R : Symbol(TA5R, Decl(intrinsicTypes.ts, 72, 26)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA5LR = Add; // never +>TA5LR : Symbol(TA5LR, Decl(intrinsicTypes.ts, 73, 26)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA6L = Add<'4', 2>; // Error +>TA6L : Symbol(TA6L, Decl(intrinsicTypes.ts, 74, 31)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA6R = Add<4, '2'>; // Error +>TA6R : Symbol(TA6R, Decl(intrinsicTypes.ts, 75, 24)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TA6LR = Add<'4', '2'>; // Error +>TA6LR : Symbol(TA6LR, Decl(intrinsicTypes.ts, 76, 24)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) + +type TS1 = Subtract<4, 2>; // 2 +>TS1 : Symbol(TS1, Decl(intrinsicTypes.ts, 77, 27)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS2L = Subtract<4 | 5, 2>; // 2 | 3 +>TS2L : Symbol(TS2L, Decl(intrinsicTypes.ts, 79, 26)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS2R = Subtract<4, 2 | 3>; // 2 | 1 +>TS2R : Symbol(TS2R, Decl(intrinsicTypes.ts, 80, 31)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS2LR = Subtract<4 | 5, 2 | 3>; // 2 | 1 | 3 +>TS2LR : Symbol(TS2LR, Decl(intrinsicTypes.ts, 81, 31)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS3L = Subtract; // number +>TS3L : Symbol(TS3L, Decl(intrinsicTypes.ts, 82, 36)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS3R = Subtract<4, number>; // number +>TS3R : Symbol(TS3R, Decl(intrinsicTypes.ts, 83, 32)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS3LR = Subtract; // number +>TS3LR : Symbol(TS3LR, Decl(intrinsicTypes.ts, 84, 32)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS4L = Subtract; // any +>TS4L : Symbol(TS4L, Decl(intrinsicTypes.ts, 85, 38)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS4R = Subtract<4, any>; // any +>TS4R : Symbol(TS4R, Decl(intrinsicTypes.ts, 86, 29)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS4LR = Subtract; // any +>TS4LR : Symbol(TS4LR, Decl(intrinsicTypes.ts, 87, 29)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS5L = Subtract; // never +>TS5L : Symbol(TS5L, Decl(intrinsicTypes.ts, 88, 32)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS5R = Subtract<4, never>; // never +>TS5R : Symbol(TS5R, Decl(intrinsicTypes.ts, 89, 31)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS5LR = Subtract; // never +>TS5LR : Symbol(TS5LR, Decl(intrinsicTypes.ts, 90, 31)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS6L = Subtract<'4', 2>; // Error +>TS6L : Symbol(TS6L, Decl(intrinsicTypes.ts, 91, 36)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS6R = Subtract<4, '2'>; // Error +>TS6R : Symbol(TS6R, Decl(intrinsicTypes.ts, 92, 29)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TS6LR = Subtract<'4', '2'>; // Error +>TS6LR : Symbol(TS6LR, Decl(intrinsicTypes.ts, 93, 29)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) + +type TM1 = Multiply<4, 2>; // 8 +>TM1 : Symbol(TM1, Decl(intrinsicTypes.ts, 94, 32)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM2L = Multiply<4 | 5, 2>; // 8 | 10 +>TM2L : Symbol(TM2L, Decl(intrinsicTypes.ts, 96, 26)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM2R = Multiply<4, 2 | 3>; // 8 | 12 +>TM2R : Symbol(TM2R, Decl(intrinsicTypes.ts, 97, 31)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM2LR = Multiply<4 | 5, 2 | 3>; // 8 | 12 | 10 | 15 +>TM2LR : Symbol(TM2LR, Decl(intrinsicTypes.ts, 98, 31)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM3L = Multiply; // number +>TM3L : Symbol(TM3L, Decl(intrinsicTypes.ts, 99, 36)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM3R = Multiply<4, number>; // number +>TM3R : Symbol(TM3R, Decl(intrinsicTypes.ts, 100, 32)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM3LR = Multiply; // number +>TM3LR : Symbol(TM3LR, Decl(intrinsicTypes.ts, 101, 32)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM4L = Multiply; // any +>TM4L : Symbol(TM4L, Decl(intrinsicTypes.ts, 102, 38)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM4R = Multiply<4, any>; // any +>TM4R : Symbol(TM4R, Decl(intrinsicTypes.ts, 103, 29)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM4LR = Multiply; // any +>TM4LR : Symbol(TM4LR, Decl(intrinsicTypes.ts, 104, 29)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM5L = Multiply; // never +>TM5L : Symbol(TM5L, Decl(intrinsicTypes.ts, 105, 32)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM5R = Multiply<4, never>; // never +>TM5R : Symbol(TM5R, Decl(intrinsicTypes.ts, 106, 31)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM5LR = Multiply; // never +>TM5LR : Symbol(TM5LR, Decl(intrinsicTypes.ts, 107, 31)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM6L = Multiply<'4', 2>; // Error +>TM6L : Symbol(TM6L, Decl(intrinsicTypes.ts, 108, 36)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM6R = Multiply<4, '2'>; // Error +>TM6R : Symbol(TM6R, Decl(intrinsicTypes.ts, 109, 29)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TM6LR = Multiply<'4', '2'>; // Error +>TM6LR : Symbol(TM6LR, Decl(intrinsicTypes.ts, 110, 29)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +type TD1 = Divide<4, 2>; // 2 +>TD1 : Symbol(TD1, Decl(intrinsicTypes.ts, 111, 32)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD2L = Divide<4 | 5, 2>; // 2 | 2.5 +>TD2L : Symbol(TD2L, Decl(intrinsicTypes.ts, 113, 24)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD2R = Divide<4, 2 | 4>; // 2 | 1 +>TD2R : Symbol(TD2R, Decl(intrinsicTypes.ts, 114, 29)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD2LR = Divide<4 | 5, 2 | 4>; // 2 | 1 | 2.5 | 1.25 +>TD2LR : Symbol(TD2LR, Decl(intrinsicTypes.ts, 115, 29)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD3L = Divide; // number +>TD3L : Symbol(TD3L, Decl(intrinsicTypes.ts, 116, 34)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD3R = Divide<4, number>; // number +>TD3R : Symbol(TD3R, Decl(intrinsicTypes.ts, 117, 30)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD3LR = Divide; // number +>TD3LR : Symbol(TD3LR, Decl(intrinsicTypes.ts, 118, 30)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD4L = Divide; // any +>TD4L : Symbol(TD4L, Decl(intrinsicTypes.ts, 119, 36)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD4R = Divide<4, any>; // any +>TD4R : Symbol(TD4R, Decl(intrinsicTypes.ts, 120, 27)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD4LR = Divide; // any +>TD4LR : Symbol(TD4LR, Decl(intrinsicTypes.ts, 121, 27)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD5L = Divide; // never +>TD5L : Symbol(TD5L, Decl(intrinsicTypes.ts, 122, 30)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD5R = Divide<4, never>; // never +>TD5R : Symbol(TD5R, Decl(intrinsicTypes.ts, 123, 29)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD5LR = Divide; // never +>TD5LR : Symbol(TD5LR, Decl(intrinsicTypes.ts, 124, 29)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD6L = Divide<'4', 2>; // Error +>TD6L : Symbol(TD6L, Decl(intrinsicTypes.ts, 125, 34)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD6R = Divide<4, '2'>; // Error +>TD6R : Symbol(TD6R, Decl(intrinsicTypes.ts, 126, 27)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD6LR = Divide<'4', '2'>; // Error +>TD6LR : Symbol(TD6LR, Decl(intrinsicTypes.ts, 127, 27)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TD7 = Divide<1, 0>; // never +>TD7 : Symbol(TD7, Decl(intrinsicTypes.ts, 128, 30)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) + +type TIX1 = Integer; +>TIX1 : Symbol(TIX1, Decl(intrinsicTypes.ts, 129, 24)) +>S : Symbol(S, Decl(intrinsicTypes.ts, 131, 10)) +>Integer : Symbol(Integer, Decl(lib.es5.d.ts, --, --)) +>S : Symbol(S, Decl(intrinsicTypes.ts, 131, 10)) + +type TIX2 = TIX1<4.2>; // 4 +>TIX2 : Symbol(TIX2, Decl(intrinsicTypes.ts, 131, 41)) +>TIX1 : Symbol(TIX1, Decl(intrinsicTypes.ts, 129, 24)) + +type TAX1 = Add; +>TAX1 : Symbol(TAX1, Decl(intrinsicTypes.ts, 132, 22)) +>M : Symbol(M, Decl(intrinsicTypes.ts, 133, 10)) +>N : Symbol(N, Decl(intrinsicTypes.ts, 133, 27)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) +>M : Symbol(M, Decl(intrinsicTypes.ts, 133, 10)) +>N : Symbol(N, Decl(intrinsicTypes.ts, 133, 27)) + +type TAX2 = TAX1<4, 2>; // 6 +>TAX2 : Symbol(TAX2, Decl(intrinsicTypes.ts, 133, 58)) +>TAX1 : Symbol(TAX1, Decl(intrinsicTypes.ts, 132, 22)) + +type TSX1 = Subtract; +>TSX1 : Symbol(TSX1, Decl(intrinsicTypes.ts, 134, 23)) +>M : Symbol(M, Decl(intrinsicTypes.ts, 135, 10)) +>N : Symbol(N, Decl(intrinsicTypes.ts, 135, 27)) +>Subtract : Symbol(Subtract, Decl(lib.es5.d.ts, --, --)) +>M : Symbol(M, Decl(intrinsicTypes.ts, 135, 10)) +>N : Symbol(N, Decl(intrinsicTypes.ts, 135, 27)) + +type TSX2 = TSX1<4, 2>; // 6 +>TSX2 : Symbol(TSX2, Decl(intrinsicTypes.ts, 135, 63)) +>TSX1 : Symbol(TSX1, Decl(intrinsicTypes.ts, 134, 23)) + +type TMX1 = Multiply; +>TMX1 : Symbol(TMX1, Decl(intrinsicTypes.ts, 136, 23)) +>M : Symbol(M, Decl(intrinsicTypes.ts, 137, 10)) +>N : Symbol(N, Decl(intrinsicTypes.ts, 137, 27)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) +>M : Symbol(M, Decl(intrinsicTypes.ts, 137, 10)) +>N : Symbol(N, Decl(intrinsicTypes.ts, 137, 27)) + +type TMX2 = TMX1<4, 2>; // 8 +>TMX2 : Symbol(TMX2, Decl(intrinsicTypes.ts, 137, 63)) +>TMX1 : Symbol(TMX1, Decl(intrinsicTypes.ts, 136, 23)) + +type TDX1 = Divide; +>TDX1 : Symbol(TDX1, Decl(intrinsicTypes.ts, 138, 23)) +>M : Symbol(M, Decl(intrinsicTypes.ts, 139, 10)) +>N : Symbol(N, Decl(intrinsicTypes.ts, 139, 27)) +>Divide : Symbol(Divide, Decl(lib.es5.d.ts, --, --)) +>M : Symbol(M, Decl(intrinsicTypes.ts, 139, 10)) +>N : Symbol(N, Decl(intrinsicTypes.ts, 139, 27)) + +type TDX2 = TDX1<4, 2>; // 2 +>TDX2 : Symbol(TDX2, Decl(intrinsicTypes.ts, 139, 61)) +>TDX1 : Symbol(TDX1, Decl(intrinsicTypes.ts, 138, 23)) + +type TAMX = Add<2, Multiply<5, 8>> // 42 +>TAMX : Symbol(TAMX, Decl(intrinsicTypes.ts, 140, 23)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) + +function foo5(s: number, x: Add, y: Multiply) { +>foo5 : Symbol(foo5, Decl(intrinsicTypes.ts, 141, 34)) +>T : Symbol(T, Decl(intrinsicTypes.ts, 143, 14)) +>U : Symbol(U, Decl(intrinsicTypes.ts, 143, 31)) +>T : Symbol(T, Decl(intrinsicTypes.ts, 143, 14)) +>s : Symbol(s, Decl(intrinsicTypes.ts, 143, 45)) +>x : Symbol(x, Decl(intrinsicTypes.ts, 143, 55)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(intrinsicTypes.ts, 143, 14)) +>U : Symbol(U, Decl(intrinsicTypes.ts, 143, 31)) +>y : Symbol(y, Decl(intrinsicTypes.ts, 143, 69)) +>Multiply : Symbol(Multiply, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(intrinsicTypes.ts, 143, 14)) +>U : Symbol(U, Decl(intrinsicTypes.ts, 143, 31)) + + s = x; +>s : Symbol(s, Decl(intrinsicTypes.ts, 143, 45)) +>x : Symbol(x, Decl(intrinsicTypes.ts, 143, 55)) + + s = y; +>s : Symbol(s, Decl(intrinsicTypes.ts, 143, 45)) +>y : Symbol(y, Decl(intrinsicTypes.ts, 143, 69)) + + x = s; // Error +>x : Symbol(x, Decl(intrinsicTypes.ts, 143, 55)) +>s : Symbol(s, Decl(intrinsicTypes.ts, 143, 45)) + + x = y; // Error +>x : Symbol(x, Decl(intrinsicTypes.ts, 143, 55)) +>y : Symbol(y, Decl(intrinsicTypes.ts, 143, 69)) + + y = s; // Error +>y : Symbol(y, Decl(intrinsicTypes.ts, 143, 69)) +>s : Symbol(s, Decl(intrinsicTypes.ts, 143, 45)) + + y = x; // Error +>y : Symbol(y, Decl(intrinsicTypes.ts, 143, 69)) +>x : Symbol(x, Decl(intrinsicTypes.ts, 143, 55)) +} + +function foo6(x: Add) { +>foo6 : Symbol(foo6, Decl(intrinsicTypes.ts, 150, 1)) +>T : Symbol(T, Decl(intrinsicTypes.ts, 152, 14)) +>x : Symbol(x, Decl(intrinsicTypes.ts, 152, 31)) +>Add : Symbol(Add, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(intrinsicTypes.ts, 152, 14)) + + let s: 3 | 4 = x; +>s : Symbol(s, Decl(intrinsicTypes.ts, 153, 7)) +>x : Symbol(x, Decl(intrinsicTypes.ts, 152, 31)) +} + +declare function foo7(x: Integer): T; +>foo7 : Symbol(foo7, Decl(intrinsicTypes.ts, 154, 1)) +>T : Symbol(T, Decl(intrinsicTypes.ts, 156, 22)) +>x : Symbol(x, Decl(intrinsicTypes.ts, 156, 40)) +>Integer : Symbol(Integer, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(intrinsicTypes.ts, 156, 22)) +>T : Symbol(T, Decl(intrinsicTypes.ts, 156, 22)) + +function foo8(x: Integer) { +>foo8 : Symbol(foo8, Decl(intrinsicTypes.ts, 156, 58)) +>U : Symbol(U, Decl(intrinsicTypes.ts, 158, 14)) +>x : Symbol(x, Decl(intrinsicTypes.ts, 158, 32)) +>Integer : Symbol(Integer, Decl(lib.es5.d.ts, --, --)) +>U : Symbol(U, Decl(intrinsicTypes.ts, 158, 14)) + + return foo7(x); +>foo7 : Symbol(foo7, Decl(intrinsicTypes.ts, 154, 1)) +>x : Symbol(x, Decl(intrinsicTypes.ts, 158, 32)) +} + diff --git a/tests/baselines/reference/intrinsicTypes.types b/tests/baselines/reference/intrinsicTypes.types index 00123d307e0f7..990c3090fe662 100644 --- a/tests/baselines/reference/intrinsicTypes.types +++ b/tests/baselines/reference/intrinsicTypes.types @@ -149,3 +149,309 @@ function foo4(x: Uppercase) { >x : Uppercase } +type TI1 = Integer<3.5>; // 3 +>TI1 : 3 + +type TI2 = Integer<2.5 | 3.4>; // 2 | 3 +>TI2 : 3 | 2 + +type TI3 = Integer; // number +>TI3 : number + +type TI4 = Integer; // any +>TI4 : any + +type TI5 = Integer; // never +>TI5 : never + +type TI6 = Integer<'42'>; // Error +>TI6 : "42" + +type TA1 = Add<4, 2>; // 6 +>TA1 : 6 + +type TA2L = Add<4 | 5, 2>; // 6 | 7 +>TA2L : 6 | 7 + +type TA2R = Add<4, 2 | 3>; // 6 | 7 +>TA2R : 6 | 7 + +type TA2LR = Add<4 | 5, 2 | 3>; // 6 | 7 | 8 +>TA2LR : 6 | 7 | 8 + +type TA3L = Add; // number +>TA3L : number + +type TA3R = Add<4, number>; // number +>TA3R : number + +type TA3LR = Add; // number +>TA3LR : number + +type TA4L = Add; // any +>TA4L : any + +type TA4R = Add<4, any>; // any +>TA4R : any + +type TA4LR = Add; // any +>TA4LR : any + +type TA5L = Add; // never +>TA5L : never + +type TA5R = Add<4, never>; // never +>TA5R : never + +type TA5LR = Add; // never +>TA5LR : never + +type TA6L = Add<'4', 2>; // Error +>TA6L : "4" + +type TA6R = Add<4, '2'>; // Error +>TA6R : "2" + +type TA6LR = Add<'4', '2'>; // Error +>TA6LR : "4" + +type TS1 = Subtract<4, 2>; // 2 +>TS1 : 2 + +type TS2L = Subtract<4 | 5, 2>; // 2 | 3 +>TS2L : 3 | 2 + +type TS2R = Subtract<4, 2 | 3>; // 2 | 1 +>TS2R : 2 | 1 + +type TS2LR = Subtract<4 | 5, 2 | 3>; // 2 | 1 | 3 +>TS2LR : 3 | 2 | 1 + +type TS3L = Subtract; // number +>TS3L : number + +type TS3R = Subtract<4, number>; // number +>TS3R : number + +type TS3LR = Subtract; // number +>TS3LR : number + +type TS4L = Subtract; // any +>TS4L : any + +type TS4R = Subtract<4, any>; // any +>TS4R : any + +type TS4LR = Subtract; // any +>TS4LR : any + +type TS5L = Subtract; // never +>TS5L : never + +type TS5R = Subtract<4, never>; // never +>TS5R : never + +type TS5LR = Subtract; // never +>TS5LR : never + +type TS6L = Subtract<'4', 2>; // Error +>TS6L : "4" + +type TS6R = Subtract<4, '2'>; // Error +>TS6R : "2" + +type TS6LR = Subtract<'4', '2'>; // Error +>TS6LR : "4" + +type TM1 = Multiply<4, 2>; // 8 +>TM1 : 8 + +type TM2L = Multiply<4 | 5, 2>; // 8 | 10 +>TM2L : 8 | 10 + +type TM2R = Multiply<4, 2 | 3>; // 8 | 12 +>TM2R : 8 | 12 + +type TM2LR = Multiply<4 | 5, 2 | 3>; // 8 | 12 | 10 | 15 +>TM2LR : 8 | 10 | 12 | 15 + +type TM3L = Multiply; // number +>TM3L : number + +type TM3R = Multiply<4, number>; // number +>TM3R : number + +type TM3LR = Multiply; // number +>TM3LR : number + +type TM4L = Multiply; // any +>TM4L : any + +type TM4R = Multiply<4, any>; // any +>TM4R : any + +type TM4LR = Multiply; // any +>TM4LR : any + +type TM5L = Multiply; // never +>TM5L : never + +type TM5R = Multiply<4, never>; // never +>TM5R : never + +type TM5LR = Multiply; // never +>TM5LR : never + +type TM6L = Multiply<'4', 2>; // Error +>TM6L : "4" + +type TM6R = Multiply<4, '2'>; // Error +>TM6R : "2" + +type TM6LR = Multiply<'4', '2'>; // Error +>TM6LR : "4" + +type TD1 = Divide<4, 2>; // 2 +>TD1 : 2 + +type TD2L = Divide<4 | 5, 2>; // 2 | 2.5 +>TD2L : 2.5 | 2 + +type TD2R = Divide<4, 2 | 4>; // 2 | 1 +>TD2R : 2 | 1 + +type TD2LR = Divide<4 | 5, 2 | 4>; // 2 | 1 | 2.5 | 1.25 +>TD2LR : 2.5 | 2 | 1 | 1.25 + +type TD3L = Divide; // number +>TD3L : number + +type TD3R = Divide<4, number>; // number +>TD3R : number + +type TD3LR = Divide; // number +>TD3LR : number + +type TD4L = Divide; // any +>TD4L : any + +type TD4R = Divide<4, any>; // any +>TD4R : any + +type TD4LR = Divide; // any +>TD4LR : any + +type TD5L = Divide; // never +>TD5L : never + +type TD5R = Divide<4, never>; // never +>TD5R : never + +type TD5LR = Divide; // never +>TD5LR : never + +type TD6L = Divide<'4', 2>; // Error +>TD6L : "4" + +type TD6R = Divide<4, '2'>; // Error +>TD6R : "2" + +type TD6LR = Divide<'4', '2'>; // Error +>TD6LR : "4" + +type TD7 = Divide<1, 0>; // never +>TD7 : never + +type TIX1 = Integer; +>TIX1 : Integer + +type TIX2 = TIX1<4.2>; // 4 +>TIX2 : 4 + +type TAX1 = Add; +>TAX1 : Add + +type TAX2 = TAX1<4, 2>; // 6 +>TAX2 : 6 + +type TSX1 = Subtract; +>TSX1 : Subtract + +type TSX2 = TSX1<4, 2>; // 6 +>TSX2 : 2 + +type TMX1 = Multiply; +>TMX1 : Multiply + +type TMX2 = TMX1<4, 2>; // 8 +>TMX2 : 8 + +type TDX1 = Divide; +>TDX1 : Divide + +type TDX2 = TDX1<4, 2>; // 2 +>TDX2 : 2 + +type TAMX = Add<2, Multiply<5, 8>> // 42 +>TAMX : 42 + +function foo5(s: number, x: Add, y: Multiply) { +>foo5 : (s: number, x: Add, y: Multiply) => void +>s : number +>x : Add +>y : Multiply + + s = x; +>s = x : Add +>s : number +>x : Add + + s = y; +>s = y : Multiply +>s : number +>y : Multiply + + x = s; // Error +>x = s : number +>x : Add +>s : number + + x = y; // Error +>x = y : Multiply +>x : Add +>y : Multiply + + y = s; // Error +>y = s : number +>y : Multiply +>s : number + + y = x; // Error +>y = x : Add +>y : Multiply +>x : Add +} + +function foo6(x: Add) { +>foo6 : (x: Add) => void +>x : Add + + let s: 3 | 4 = x; +>s : 3 | 4 +>x : 3 | 4 +} + +declare function foo7(x: Integer): T; +>foo7 : (x: Integer) => T +>x : Integer + +function foo8(x: Integer) { +>foo8 : (x: Integer) => U +>x : Integer + + return foo7(x); +>foo7(x) : U +>foo7 : (x: Integer) => T +>x : Integer +} + diff --git a/tests/baselines/reference/jsDeclarationsJSDocRedirectedLookups.js b/tests/baselines/reference/jsDeclarationsJSDocRedirectedLookups.js index a56944c921d6d..b51646c19a5c2 100644 --- a/tests/baselines/reference/jsDeclarationsJSDocRedirectedLookups.js +++ b/tests/baselines/reference/jsDeclarationsJSDocRedirectedLookups.js @@ -89,7 +89,7 @@ out/index.d.ts(14,39): error TS2304: Cannot find name 'class'. out/index.d.ts(15,38): error TS2304: Cannot find name 'bool'. out/index.d.ts(16,37): error TS2304: Cannot find name 'int'. out/index.d.ts(17,39): error TS2304: Cannot find name 'float'. -out/index.d.ts(18,41): error TS2304: Cannot find name 'integer'. +out/index.d.ts(18,41): error TS2552: Cannot find name 'integer'. Did you mean 'Integer'? ==== ./out/index.d.ts (5 errors) ==== @@ -120,6 +120,6 @@ out/index.d.ts(18,41): error TS2304: Cannot find name 'integer'. !!! error TS2304: Cannot find name 'float'. /** @type {integer} */ declare const p: integer; ~~~~~~~ -!!! error TS2304: Cannot find name 'integer'. +!!! error TS2552: Cannot find name 'integer'. Did you mean 'Integer'? /** @type {event} */ declare const q: Event | undefined; \ No newline at end of file diff --git a/tests/baselines/reference/recursiveConditionalTypes.errors.txt b/tests/baselines/reference/recursiveConditionalTypes.errors.txt index 3706310c55970..08680e4911ac3 100644 --- a/tests/baselines/reference/recursiveConditionalTypes.errors.txt +++ b/tests/baselines/reference/recursiveConditionalTypes.errors.txt @@ -205,10 +205,10 @@ tests/cases/compiler/recursiveConditionalTypes.ts(169,5): error TS2322: Type 'nu type NTuple = Tup['length'] extends N ? Tup : NTuple; - type Add = + type AddTuples = [...NTuple, ...NTuple]['length']; - let five: Add<2, 3>; + let five: AddTuples<2, 3>; // Repro from #46316 diff --git a/tests/baselines/reference/recursiveConditionalTypes.js b/tests/baselines/reference/recursiveConditionalTypes.js index 0c59030955060..250569764045d 100644 --- a/tests/baselines/reference/recursiveConditionalTypes.js +++ b/tests/baselines/reference/recursiveConditionalTypes.js @@ -143,10 +143,10 @@ type TP2 = ParseManyWhitespace2<" foo">; type NTuple = Tup['length'] extends N ? Tup : NTuple; -type Add = +type AddTuples = [...NTuple, ...NTuple]['length']; -let five: Add<2, 3>; +let five: AddTuples<2, 3>; // Repro from #46316 @@ -281,11 +281,11 @@ declare type ParseManyWhitespace2 = S extends ` ${infer R0}` ? declare type Helper = T extends ParseSuccess ? ParseSuccess : null; declare type TP2 = ParseManyWhitespace2<" foo">; declare type NTuple = Tup['length'] extends N ? Tup : NTuple; -declare type Add = [ +declare type AddTuples = [ ...NTuple, ...NTuple ]['length']; -declare let five: Add<2, 3>; +declare let five: AddTuples<2, 3>; declare type _PrependNextNum> = A['length'] extends infer T ? [T, ...A] extends [...infer X] ? X : never : never; declare type _Enumerate, N extends number> = N extends A['length'] ? A : _Enumerate<_PrependNextNum, N> & number; declare type Enumerate = number extends N ? number : _Enumerate<[], N> extends (infer E)[] ? E : never; diff --git a/tests/baselines/reference/recursiveConditionalTypes.symbols b/tests/baselines/reference/recursiveConditionalTypes.symbols index e9b58df3c28a3..73ee5d1a4ef89 100644 --- a/tests/baselines/reference/recursiveConditionalTypes.symbols +++ b/tests/baselines/reference/recursiveConditionalTypes.symbols @@ -546,25 +546,25 @@ type NTuple = >N : Symbol(N, Decl(recursiveConditionalTypes.ts, 141, 12)) >Tup : Symbol(Tup, Decl(recursiveConditionalTypes.ts, 141, 29)) -type Add = ->Add : Symbol(Add, Decl(recursiveConditionalTypes.ts, 142, 65)) ->A : Symbol(A, Decl(recursiveConditionalTypes.ts, 144, 9)) ->B : Symbol(B, Decl(recursiveConditionalTypes.ts, 144, 26)) +type AddTuples = +>AddTuples : Symbol(AddTuples, Decl(recursiveConditionalTypes.ts, 142, 65)) +>A : Symbol(A, Decl(recursiveConditionalTypes.ts, 144, 15)) +>B : Symbol(B, Decl(recursiveConditionalTypes.ts, 144, 32)) [...NTuple, ...NTuple]['length']; >NTuple : Symbol(NTuple, Decl(recursiveConditionalTypes.ts, 137, 40)) ->A : Symbol(A, Decl(recursiveConditionalTypes.ts, 144, 9)) +>A : Symbol(A, Decl(recursiveConditionalTypes.ts, 144, 15)) >NTuple : Symbol(NTuple, Decl(recursiveConditionalTypes.ts, 137, 40)) ->B : Symbol(B, Decl(recursiveConditionalTypes.ts, 144, 26)) +>B : Symbol(B, Decl(recursiveConditionalTypes.ts, 144, 32)) -let five: Add<2, 3>; +let five: AddTuples<2, 3>; >five : Symbol(five, Decl(recursiveConditionalTypes.ts, 147, 3)) ->Add : Symbol(Add, Decl(recursiveConditionalTypes.ts, 142, 65)) +>AddTuples : Symbol(AddTuples, Decl(recursiveConditionalTypes.ts, 142, 65)) // Repro from #46316 type _PrependNextNum> = A['length'] extends infer T ->_PrependNextNum : Symbol(_PrependNextNum, Decl(recursiveConditionalTypes.ts, 147, 20)) +>_PrependNextNum : Symbol(_PrependNextNum, Decl(recursiveConditionalTypes.ts, 147, 26)) >A : Symbol(A, Decl(recursiveConditionalTypes.ts, 151, 21)) >Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --) ... and 3 more) >A : Symbol(A, Decl(recursiveConditionalTypes.ts, 151, 21)) @@ -594,7 +594,7 @@ type _Enumerate, N extends number> = N extends A['lengt : _Enumerate<_PrependNextNum, N> & number; >_Enumerate : Symbol(_Enumerate, Decl(recursiveConditionalTypes.ts, 155, 12)) ->_PrependNextNum : Symbol(_PrependNextNum, Decl(recursiveConditionalTypes.ts, 147, 20)) +>_PrependNextNum : Symbol(_PrependNextNum, Decl(recursiveConditionalTypes.ts, 147, 26)) >A : Symbol(A, Decl(recursiveConditionalTypes.ts, 157, 16)) >N : Symbol(N, Decl(recursiveConditionalTypes.ts, 157, 41)) diff --git a/tests/baselines/reference/recursiveConditionalTypes.types b/tests/baselines/reference/recursiveConditionalTypes.types index 2291ce8b5eb35..ed95f9ea4ae29 100644 --- a/tests/baselines/reference/recursiveConditionalTypes.types +++ b/tests/baselines/reference/recursiveConditionalTypes.types @@ -352,12 +352,12 @@ type NTuple = Tup['length'] extends N ? Tup : NTuple; -type Add = ->Add : Add +type AddTuples = +>AddTuples : AddTuples [...NTuple, ...NTuple]['length']; -let five: Add<2, 3>; +let five: AddTuples<2, 3>; >five : 5 // Repro from #46316 diff --git a/tests/cases/compiler/recursiveConditionalTypes.ts b/tests/cases/compiler/recursiveConditionalTypes.ts index 6810a37ca072f..56c0b4cb1afc4 100644 --- a/tests/cases/compiler/recursiveConditionalTypes.ts +++ b/tests/cases/compiler/recursiveConditionalTypes.ts @@ -146,10 +146,10 @@ type TP2 = ParseManyWhitespace2<" foo">; type NTuple = Tup['length'] extends N ? Tup : NTuple; -type Add = +type AddTuples = [...NTuple, ...NTuple]['length']; -let five: Add<2, 3>; +let five: AddTuples<2, 3>; // Repro from #46316 diff --git a/tests/cases/conformance/types/typeAliases/intrinsicTypes.ts b/tests/cases/conformance/types/typeAliases/intrinsicTypes.ts index cfdf08d0609f0..57a3642d306b4 100644 --- a/tests/cases/conformance/types/typeAliases/intrinsicTypes.ts +++ b/tests/cases/conformance/types/typeAliases/intrinsicTypes.ts @@ -55,3 +55,110 @@ declare function foo3(x: Uppercase): T; function foo4(x: Uppercase) { return foo3(x); } + +type TI1 = Integer<3.5>; // 3 +type TI2 = Integer<2.5 | 3.4>; // 2 | 3 +type TI3 = Integer; // number +type TI4 = Integer; // any +type TI5 = Integer; // never +type TI6 = Integer<'42'>; // Error + +type TA1 = Add<4, 2>; // 6 +type TA2L = Add<4 | 5, 2>; // 6 | 7 +type TA2R = Add<4, 2 | 3>; // 6 | 7 +type TA2LR = Add<4 | 5, 2 | 3>; // 6 | 7 | 8 +type TA3L = Add; // number +type TA3R = Add<4, number>; // number +type TA3LR = Add; // number +type TA4L = Add; // any +type TA4R = Add<4, any>; // any +type TA4LR = Add; // any +type TA5L = Add; // never +type TA5R = Add<4, never>; // never +type TA5LR = Add; // never +type TA6L = Add<'4', 2>; // Error +type TA6R = Add<4, '2'>; // Error +type TA6LR = Add<'4', '2'>; // Error + +type TS1 = Subtract<4, 2>; // 2 +type TS2L = Subtract<4 | 5, 2>; // 2 | 3 +type TS2R = Subtract<4, 2 | 3>; // 2 | 1 +type TS2LR = Subtract<4 | 5, 2 | 3>; // 2 | 1 | 3 +type TS3L = Subtract; // number +type TS3R = Subtract<4, number>; // number +type TS3LR = Subtract; // number +type TS4L = Subtract; // any +type TS4R = Subtract<4, any>; // any +type TS4LR = Subtract; // any +type TS5L = Subtract; // never +type TS5R = Subtract<4, never>; // never +type TS5LR = Subtract; // never +type TS6L = Subtract<'4', 2>; // Error +type TS6R = Subtract<4, '2'>; // Error +type TS6LR = Subtract<'4', '2'>; // Error + +type TM1 = Multiply<4, 2>; // 8 +type TM2L = Multiply<4 | 5, 2>; // 8 | 10 +type TM2R = Multiply<4, 2 | 3>; // 8 | 12 +type TM2LR = Multiply<4 | 5, 2 | 3>; // 8 | 12 | 10 | 15 +type TM3L = Multiply; // number +type TM3R = Multiply<4, number>; // number +type TM3LR = Multiply; // number +type TM4L = Multiply; // any +type TM4R = Multiply<4, any>; // any +type TM4LR = Multiply; // any +type TM5L = Multiply; // never +type TM5R = Multiply<4, never>; // never +type TM5LR = Multiply; // never +type TM6L = Multiply<'4', 2>; // Error +type TM6R = Multiply<4, '2'>; // Error +type TM6LR = Multiply<'4', '2'>; // Error + +type TD1 = Divide<4, 2>; // 2 +type TD2L = Divide<4 | 5, 2>; // 2 | 2.5 +type TD2R = Divide<4, 2 | 4>; // 2 | 1 +type TD2LR = Divide<4 | 5, 2 | 4>; // 2 | 1 | 2.5 | 1.25 +type TD3L = Divide; // number +type TD3R = Divide<4, number>; // number +type TD3LR = Divide; // number +type TD4L = Divide; // any +type TD4R = Divide<4, any>; // any +type TD4LR = Divide; // any +type TD5L = Divide; // never +type TD5R = Divide<4, never>; // never +type TD5LR = Divide; // never +type TD6L = Divide<'4', 2>; // Error +type TD6R = Divide<4, '2'>; // Error +type TD6LR = Divide<'4', '2'>; // Error +type TD7 = Divide<1, 0>; // never + +type TIX1 = Integer; +type TIX2 = TIX1<4.2>; // 4 +type TAX1 = Add; +type TAX2 = TAX1<4, 2>; // 6 +type TSX1 = Subtract; +type TSX2 = TSX1<4, 2>; // 6 +type TMX1 = Multiply; +type TMX2 = TMX1<4, 2>; // 8 +type TDX1 = Divide; +type TDX2 = TDX1<4, 2>; // 2 +type TAMX = Add<2, Multiply<5, 8>> // 42 + +function foo5(s: number, x: Add, y: Multiply) { + s = x; + s = y; + x = s; // Error + x = y; // Error + y = s; // Error + y = x; // Error +} + +function foo6(x: Add) { + let s: 3 | 4 = x; +} + +declare function foo7(x: Integer): T; + +function foo8(x: Integer) { + return foo7(x); +}