Improve intersection reduction and CFA for truthy, equality, and typeof checks (#49119)

* Improve reduction of intersection types

* Accept new baselines

* Improve CFA for truthy, equality, and typeof checks

* Accept new baselines

* Remove special case for Function type

* Don't reduce intersections of form {...} & object

* Accept new baselines

* Anything is assignable to unknown-like union

* Accept new baselines

* Tweak subtype check

* Recombine unknown type from unknown-like union in more cases

* Display union origin only if it is shorter than union itself

* Accept new baselines

* Add tests

* Only attach origin type when it is shorter than union itself

* Specially preserve string & {}, number & {}, bigint & {}

* Accept new baselines

* Add additional tests

* Fix getNormalizedType and getNarrowableTypeForReference for intersections

* Switch NonNullable<T> to use T & {}

* Accept new baselines

* Use NonNullable<T> in place of anonymous T & {}

* Accept new baselines

* Add fourslash test

* More fourslash tests

* Fix getFalsyFlags handling of intersections

* Accept new baselines

* Add constraint to compareProperties type parameter

* Unconstrained type parameter not assignable to {} with strictNullChecks

* Accept new baselines
This commit is contained in:
Anders Hejlsberg authored and GitHub committed 2022-05-27 14:02:54 -07:00
1 parent ba4525202e
commit 51b346d65a
53 files changed
+3501 -305

No files matched your search

+137 -78
View File
@@ -848,6 +848,8 @@ namespace ts {
emptyTypeLiteralSymbol.members = createSymbolTable();
const emptyTypeLiteralType = createAnonymousType(emptyTypeLiteralSymbol, emptySymbols, emptyArray, emptyArray, emptyArray);
const unknownUnionType = strictNullChecks ? getUnionType([undefinedType, nullType, createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, emptyArray)]) : unknownType;
const emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, emptyArray) as ObjectType as GenericType;
emptyGenericType.instantiations = new Map<string, TypeReference>();
@@ -12058,7 +12060,7 @@ namespace ts {
}
}
}
else if (t.flags & TypeFlags.DisjointDomains) {
else if (t.flags & TypeFlags.DisjointDomains || isEmptyAnonymousObjectType(t)) {
hasDisjointDomainType = true;
}
}
@@ -12069,7 +12071,7 @@ namespace ts {
// We add any types belong to one of the disjoint domains because they might cause the final
// intersection operation to reduce the union constraints.
for (const t of types) {
if (t.flags & TypeFlags.DisjointDomains) {
if (t.flags & TypeFlags.DisjointDomains || isEmptyAnonymousObjectType(t)) {
constraints = append(constraints, t);
}
}
@@ -14826,7 +14828,7 @@ namespace ts {
return includes;
}
function removeRedundantPrimitiveTypes(types: Type[], includes: TypeFlags) {
function removeRedundantSupertypes(types: Type[], includes: TypeFlags) {
let i = types.length;
while (i > 0) {
i--;
@@ -14835,7 +14837,9 @@ namespace ts {
t.flags & TypeFlags.String && includes & (TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) ||
t.flags & TypeFlags.Number && includes & TypeFlags.NumberLiteral ||
t.flags & TypeFlags.BigInt && includes & TypeFlags.BigIntLiteral ||
t.flags & TypeFlags.ESSymbol && includes & TypeFlags.UniqueESSymbol;
t.flags & TypeFlags.ESSymbol && includes & TypeFlags.UniqueESSymbol ||
t.flags & TypeFlags.Void && includes & TypeFlags.Undefined ||
isEmptyAnonymousObjectType(t) && includes & TypeFlags.DefinitelyNonNullable;
if (remove) {
orderedRemoveItemAt(types, i);
}
@@ -14959,7 +14963,7 @@ namespace ts {
// a type alias of the form "type List<T> = T & { next: List<T> }" cannot be reduced during its declaration.
// Also, unlike union types, the order of the constituent types is preserved in order that overload resolution
// for intersections of types with signatures can be deterministic.
function getIntersectionType(types: readonly Type[], aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[]): Type {
function getIntersectionType(types: readonly Type[], aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[], noSupertypeReduction?: boolean): Type {
const typeMembershipMap: ESMap<string, Type> = new Map();
const includes = addTypesToIntersection(typeMembershipMap, 0, types);
const typeSet: Type[] = arrayFrom(typeMembershipMap.values());
@@ -14996,11 +15000,10 @@ namespace ts {
if (includes & TypeFlags.String && includes & (TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) ||
includes & TypeFlags.Number && includes & TypeFlags.NumberLiteral ||
includes & TypeFlags.BigInt && includes & TypeFlags.BigIntLiteral ||
includes & TypeFlags.ESSymbol && includes & TypeFlags.UniqueESSymbol) {
removeRedundantPrimitiveTypes(typeSet, includes);
}
if (includes & TypeFlags.IncludesEmptyObject && includes & TypeFlags.Object) {
orderedRemoveItemAt(typeSet, findIndex(typeSet, isEmptyAnonymousObjectType));
includes & TypeFlags.ESSymbol && includes & TypeFlags.UniqueESSymbol ||
includes & TypeFlags.Void && includes & TypeFlags.Undefined ||
includes & TypeFlags.IncludesEmptyObject && includes & TypeFlags.DefinitelyNonNullable) {
if (!noSupertypeReduction) removeRedundantSupertypes(typeSet, includes);
}
if (includes & TypeFlags.IncludesMissingType) {
typeSet[typeSet.indexOf(undefinedType)] = missingType;
@@ -15039,8 +15042,9 @@ namespace ts {
}
const constituents = getCrossProductIntersections(typeSet);
// We attach a denormalized origin type when at least one constituent of the cross-product union is an
// intersection (i.e. when the intersection didn't just reduce one or more unions to smaller unions).
const origin = some(constituents, t => !!(t.flags & TypeFlags.Intersection)) ? createOriginUnionOrIntersectionType(TypeFlags.Intersection, typeSet) : undefined;
// intersection (i.e. when the intersection didn't just reduce one or more unions to smaller unions) and
// the denormalized origin has fewer constituents than the union itself.
const origin = some(constituents, t => !!(t.flags & TypeFlags.Intersection)) && getConstituentCountOfTypes(constituents) > getConstituentCountOfTypes(typeSet) ? createOriginUnionOrIntersectionType(TypeFlags.Intersection, typeSet) : undefined;
result = getUnionType(constituents, UnionReduction.Literal, aliasSymbol, aliasTypeArguments, origin);
}
}
@@ -15086,12 +15090,23 @@ namespace ts {
return intersections;
}
function getConstituentCount(type: Type): number {
return !(type.flags & TypeFlags.UnionOrIntersection) || type.aliasSymbol ? 1 :
type.flags & TypeFlags.Union && (type as UnionType).origin ? getConstituentCount((type as UnionType).origin!) :
getConstituentCountOfTypes((type as UnionOrIntersectionType).types);
}
function getConstituentCountOfTypes(types: Type[]): number {
return reduceLeft(types, (n, t) => n + getConstituentCount(t), 0);
}
function getTypeFromIntersectionTypeNode(node: IntersectionTypeNode): Type {
const links = getNodeLinks(node);
if (!links.resolvedType) {
const aliasSymbol = getAliasSymbolForTypeNode(node);
links.resolvedType = getIntersectionType(map(node.types, getTypeFromTypeNode),
aliasSymbol, getTypeArgumentsForAliasSymbol(aliasSymbol));
const types = map(node.types, getTypeFromTypeNode);
const noSupertypeReduction = types.length === 2 && !!(types[0].flags & (TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt)) && types[1] === emptyTypeLiteralType;
links.resolvedType = getIntersectionType(types, aliasSymbol, getTypeArgumentsForAliasSymbol(aliasSymbol), noSupertypeReduction);
}
return links.resolvedType;
}
@@ -18116,6 +18131,18 @@ namespace ts {
type.symbol && type.symbol.flags & SymbolFlags.TypeLiteral && getMembersOfSymbol(type.symbol).size === 0));
}
function isUnknownLikeUnionType(type: Type) {
if (strictNullChecks && type.flags & TypeFlags.Union) {
if (!((type as UnionType).objectFlags & ObjectFlags.IsUnknownLikeUnionComputed)) {
const types = (type as UnionType).types;
(type as UnionType).objectFlags |= ObjectFlags.IsUnknownLikeUnionComputed | (types.length >= 3 && types[0].flags & TypeFlags.Undefined &&
types[1].flags & TypeFlags.Null && some(types, isEmptyAnonymousObjectType) ? ObjectFlags.IsUnknownLikeUnion : 0);
}
return !!((type as UnionType).objectFlags & ObjectFlags.IsUnknownLikeUnion);
}
return false;
}
function isStringIndexSignatureOnlyType(type: Type): boolean {
return type.flags & TypeFlags.Object && !isGenericMappedType(type) && getPropertiesOfType(type).length === 0 && getIndexInfosOfType(type).length === 1 && !!getIndexInfoOfType(type, stringType) ||
type.flags & TypeFlags.UnionOrIntersection && every((type as UnionOrIntersectionType).types, isStringIndexSignatureOnlyType) ||
@@ -18183,7 +18210,7 @@ namespace ts {
// Since unions and intersections may reduce to `never`, we exclude them here.
if (s & TypeFlags.Undefined && (!strictNullChecks && !(t & TypeFlags.UnionOrIntersection) || t & (TypeFlags.Undefined | TypeFlags.Void))) return true;
if (s & TypeFlags.Null && (!strictNullChecks && !(t & TypeFlags.UnionOrIntersection) || t & TypeFlags.Null)) return true;
if (s & TypeFlags.Object && t & TypeFlags.NonPrimitive) return true;
if (s & TypeFlags.Object && t & TypeFlags.NonPrimitive && !(relation === strictSubtypeRelation && isEmptyAnonymousObjectType(source))) return true;
if (relation === assignableRelation || relation === comparableRelation) {
if (s & TypeFlags.Any) return true;
// Type number or any numeric literal type is assignable to any numeric enum type or any
@@ -18191,6 +18218,8 @@ namespace ts {
// bit-flag enum types sometimes look like literal enum types with numeric literal values.
if (s & (TypeFlags.Number | TypeFlags.NumberLiteral) && !(s & TypeFlags.EnumLiteral) && (
t & TypeFlags.Enum || relation === assignableRelation && t & TypeFlags.NumberLiteral && t & TypeFlags.EnumLiteral)) return true;
// Anything is assignable to a union containing undefined, null, and {}
if (isUnknownLikeUnionType(target)) return true;
}
return false;
}
@@ -18233,16 +18262,28 @@ namespace ts {
function getNormalizedType(type: Type, writing: boolean): Type {
while (true) {
let t = isFreshLiteralType(type) ? (type as FreshableType).regularType :
getObjectFlags(type) & ObjectFlags.Reference && (type as TypeReference).node ? createTypeReference((type as TypeReference).target, getTypeArguments(type as TypeReference)) :
type.flags & TypeFlags.UnionOrIntersection ? getReducedType(type) :
const t = isFreshLiteralType(type) ? (type as FreshableType).regularType :
getObjectFlags(type) & ObjectFlags.Reference ? (type as TypeReference).node ? createTypeReference((type as TypeReference).target, getTypeArguments(type as TypeReference)) : getSingleBaseForNonAugmentingSubtype(type) || type :
type.flags & TypeFlags.UnionOrIntersection ? getNormalizedUnionOrIntersectionType(type as UnionOrIntersectionType, writing) :
type.flags & TypeFlags.Substitution ? writing ? (type as SubstitutionType).baseType : (type as SubstitutionType).substitute :
type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) :
type;
t = getSingleBaseForNonAugmentingSubtype(t) || t;
if (t === type) break;
if (t === type) return t;
type = t;
}
}
function getNormalizedUnionOrIntersectionType(type: UnionOrIntersectionType, writing: boolean) {
const reduced = getReducedType(type);
if (reduced !== type) {
return reduced;
}
if (type.flags & TypeFlags.Intersection) {
const normalizedTypes = sameMap(type.types, t => getNormalizedType(t, writing));
if (normalizedTypes !== type.types) {
return getIntersectionType(normalizedTypes);
}
}
return type;
}
@@ -18772,6 +18813,9 @@ namespace ts {
return;
}
reportRelationError(headMessage, source, target);
if (strictNullChecks && source.flags & TypeFlags.TypeVariable && source.symbol?.declarations?.[0] && !getConstraintOfType(source as TypeVariable) && isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) {
associateRelatedInfo(createDiagnosticForNode(source.symbol.declarations[0], Diagnostics.This_type_parameter_probably_needs_an_extends_object_constraint));
}
}
function traceUnionsOrIntersectionsTooLarge(source: Type, target: Type): void {
@@ -19576,13 +19620,6 @@ namespace ts {
// IndexedAccess comparisons are handled above in the `targetFlags & TypeFlage.IndexedAccess` branch
if (!(sourceFlags & TypeFlags.IndexedAccess && targetFlags & TypeFlags.IndexedAccess)) {
const constraint = getConstraintOfType(source as TypeVariable) || unknownType;
if (!getConstraintOfType(source as TypeVariable) || (sourceFlags & TypeFlags.TypeParameter && constraint.flags & TypeFlags.Any)) {
// A type variable with no constraint is not related to the non-primitive object type.
if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive), RecursionFlags.Both)) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
// hi-speed no-this-instantiation check (less accurate, but avoids costly `this`-instantiation when the constraint will suffice), see #28231 for report on why this is needed
if (result = isRelatedTo(constraint, target, RecursionFlags.Source, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) {
resetErrorInfo(saveErrorInfo);
@@ -21106,9 +21143,11 @@ namespace ts {
return getSupertypeOrUnion(types);
}
const primaryTypes = filter(types, t => !(t.flags & TypeFlags.Nullable));
return primaryTypes.length ?
getNullableType(getSupertypeOrUnion(primaryTypes), getFalsyFlagsOfTypes(types) & TypeFlags.Nullable) :
getUnionType(types, UnionReduction.Subtype);
if (primaryTypes.length) {
const supertypeOrUnion = getSupertypeOrUnion(primaryTypes);
return primaryTypes === types ? supertypeOrUnion : getUnionType([supertypeOrUnion, ...filter(types, t => !!(t.flags & TypeFlags.Nullable))]);
}
return getUnionType(types, UnionReduction.Subtype);
}
// Return the leftmost type for which no type to the right is a subtype.
@@ -21337,12 +21376,13 @@ namespace ts {
// flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns
// no flags for all other types (including non-falsy literal types).
function getFalsyFlags(type: Type): TypeFlags {
return type.flags & TypeFlags.Union ? getFalsyFlagsOfTypes((type as UnionType).types) :
type.flags & TypeFlags.StringLiteral ? (type as StringLiteralType).value === "" ? TypeFlags.StringLiteral : 0 :
type.flags & TypeFlags.NumberLiteral ? (type as NumberLiteralType).value === 0 ? TypeFlags.NumberLiteral : 0 :
type.flags & TypeFlags.BigIntLiteral ? isZeroBigInt(type as BigIntLiteralType) ? TypeFlags.BigIntLiteral : 0 :
type.flags & TypeFlags.BooleanLiteral ? (type === falseType || type === regularFalseType) ? TypeFlags.BooleanLiteral : 0 :
type.flags & TypeFlags.PossiblyFalsy;
const t = type.flags & TypeFlags.Intersection ? getBaseConstraintOrType(type) : type;
return t.flags & TypeFlags.Union ? getFalsyFlagsOfTypes((t as UnionType).types) :
t.flags & TypeFlags.StringLiteral ? (t as StringLiteralType).value === "" ? TypeFlags.StringLiteral : 0 :
t.flags & TypeFlags.NumberLiteral ? (t as NumberLiteralType).value === 0 ? TypeFlags.NumberLiteral : 0 :
t.flags & TypeFlags.BigIntLiteral ? isZeroBigInt(t as BigIntLiteralType) ? TypeFlags.BigIntLiteral : 0 :
t.flags & TypeFlags.BooleanLiteral ? (t === falseType || t === regularFalseType) ? TypeFlags.BooleanLiteral : 0 :
t.flags & TypeFlags.PossiblyFalsy;
}
function removeDefinitelyFalsyTypes(type: Type): Type {
@@ -21387,21 +21427,23 @@ namespace ts {
}
function getGlobalNonNullableTypeInstantiation(type: Type) {
// First reduce away any constituents that are assignable to 'undefined' or 'null'. This not only eliminates
// 'undefined' and 'null', but also higher-order types such as a type parameter 'U extends undefined | null'
// that isn't eliminated by a NonNullable<T> instantiation.
const reducedType = getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull);
if (!deferredGlobalNonNullableTypeAlias) {
deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable" as __String, SymbolFlags.TypeAlias, /*diagnostic*/ undefined) || unknownSymbol;
}
// If the NonNullable<T> type is available, return an instantiation. Otherwise just return the reduced type.
return deferredGlobalNonNullableTypeAlias !== unknownSymbol ?
getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [reducedType]) :
reducedType;
getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]) :
getIntersectionType([type, emptyObjectType]);
}
function getNonNullableType(type: Type): Type {
return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
if (strictNullChecks) {
// First reduce away any constituents that are assignable to 'undefined' or 'null'. This not only eliminates
// 'undefined' and 'null', but also higher-order types such as a type parameter 'U extends undefined | null'
// that isn't eliminated by a NonNullable<T> instantiation.
const reducedType = getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull);
return maybeTypeOfKind(reducedType, TypeFlags.Instantiable) ? getGlobalNonNullableTypeInstantiation(reducedType) : reducedType;
}
return type;
}
function addOptionalTypeMarker(type: Type) {
@@ -23586,6 +23628,26 @@ namespace ts {
return filterType(type, t => (getTypeFacts(t) & include) !== 0);
}
function getIntersectionWithFacts(type: Type, facts: TypeFacts) {
const reduced = recombineUnknownType(getTypeWithFacts(strictNullChecks && type.flags & TypeFlags.Unknown ? unknownUnionType : type, facts));
if (strictNullChecks) {
switch (facts) {
case TypeFacts.NEUndefined:
return mapType(reduced, t => getTypeFacts(t) & TypeFacts.EQUndefined ? getIntersectionType([t, getTypeFacts(t) & TypeFacts.EQNull && !maybeTypeOfKind(reduced, TypeFlags.Null) ? getUnionType([emptyObjectType, nullType]) : emptyObjectType]): t);
case TypeFacts.NENull:
return mapType(reduced, t => getTypeFacts(t) & TypeFacts.EQNull ? getIntersectionType([t, getTypeFacts(t) & TypeFacts.EQUndefined && !maybeTypeOfKind(reduced, TypeFlags.Undefined) ? getUnionType([emptyObjectType, undefinedType]) : emptyObjectType]): t);
case TypeFacts.NEUndefinedOrNull:
case TypeFacts.Truthy:
return mapType(reduced, t => getTypeFacts(t) & TypeFacts.EQUndefinedOrNull ? getGlobalNonNullableTypeInstantiation(t): t);
}
}
return reduced;
}
function recombineUnknownType(type: Type) {
return type === unknownUnionType ? unknownType : type;
}
function getTypeWithDefault(type: Type, defaultExpression: Expression) {
return defaultExpression ?
getUnionType([getNonUndefinedType(type), getTypeOfExpression(defaultExpression)]) :
@@ -24717,7 +24779,7 @@ namespace ts {
if (isEvolvingArrayTypeList(types)) {
return getEvolvingArrayType(getUnionType(map(types, getElementTypeOfEvolvingArrayType)));
}
const result = getUnionType(sameMap(types, finalizeEvolvingArrayType), subtypeReduction);
const result = recombineUnknownType(getUnionType(sameMap(types, finalizeEvolvingArrayType), subtypeReduction));
if (result !== declaredType && result.flags & declaredType.flags & TypeFlags.Union && arraysEqual((result as UnionType).types, (declaredType as UnionType).types)) {
return declaredType;
}
@@ -24825,8 +24887,7 @@ namespace ts {
function narrowTypeByTruthiness(type: Type, expr: Expression, assumeTrue: boolean): Type {
if (isMatchingReference(reference, expr)) {
return type.flags & TypeFlags.Unknown && assumeTrue ? nonNullUnknownType :
getTypeWithFacts(type, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy);
return getIntersectionWithFacts(type, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy);
}
if (strictNullChecks && assumeTrue && optionalChainContainsReference(expr, reference)) {
type = getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull);
@@ -24986,9 +25047,6 @@ namespace ts {
assumeTrue = !assumeTrue;
}
const valueType = getTypeOfExpression(value);
if (assumeTrue && (type.flags & TypeFlags.Unknown) && (operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken) && (valueType.flags & TypeFlags.Null)) {
return getUnionType([nullType, undefinedType]);
}
if ((type.flags & TypeFlags.Unknown) && assumeTrue && (operator === SyntaxKind.EqualsEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken)) {
if (valueType.flags & (TypeFlags.Primitive | TypeFlags.NonPrimitive)) {
return valueType;
@@ -25008,7 +25066,7 @@ namespace ts {
valueType.flags & TypeFlags.Null ?
assumeTrue ? TypeFacts.EQNull : TypeFacts.NENull :
assumeTrue ? TypeFacts.EQUndefined : TypeFacts.NEUndefined;
return type.flags & TypeFlags.Unknown && facts & (TypeFacts.NENull | TypeFacts.NEUndefinedOrNull) ? nonNullUnknownType : getTypeWithFacts(type, facts);
return getIntersectionWithFacts(type, facts);
}
if (assumeTrue) {
const filterFn: (t: Type) => boolean = operator === SyntaxKind.EqualsEqualsToken ?
@@ -25037,17 +25095,11 @@ namespace ts {
if (type.flags & TypeFlags.Any && literal.text === "function") {
return type;
}
if (assumeTrue && type.flags & TypeFlags.Unknown && literal.text === "object") {
// The non-null unknown type is used to track whether a previous narrowing operation has removed the null type
// from the unknown type. For example, the expression `x && typeof x === 'object'` first narrows x to the non-null
// unknown type, and then narrows that to the non-primitive type.
return type === nonNullUnknownType ? nonPrimitiveType : getUnionType([nonPrimitiveType, nullType]);
}
const facts = assumeTrue ?
typeofEQFacts.get(literal.text) || TypeFacts.TypeofEQHostObject :
typeofNEFacts.get(literal.text) || TypeFacts.TypeofNEHostObject;
const impliedType = getImpliedTypeFromTypeofGuard(type, literal.text);
return getTypeWithFacts(assumeTrue && impliedType ? mapType(type, narrowUnionMemberByTypeof(impliedType)) : type, facts);
return getTypeWithFacts(assumeTrue && impliedType ? narrowTypeByImpliedType(type, impliedType) : type, facts);
}
function narrowTypeBySwitchOptionalChainContainment(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number, clauseCheck: (type: Type) => boolean) {
@@ -25100,10 +25152,10 @@ namespace ts {
function getImpliedTypeFromTypeofGuard(type: Type, text: string) {
switch (text) {
case "object":
return type.flags & TypeFlags.Any ? type : getUnionType([nullType, nonPrimitiveType]);
case "function":
return type.flags & TypeFlags.Any ? type : globalFunctionType;
case "object":
return type.flags & TypeFlags.Unknown ? getUnionType([nonPrimitiveType, nullType]) : type;
default:
return typeofTypesByName.get(text);
}
@@ -25115,22 +25167,27 @@ namespace ts {
// the guard. For example: narrowing `{} | undefined` by `"boolean"` should produce the type `boolean`, not
// the filtered type `{}`. For this reason we narrow constituents of the union individually, in addition to
// filtering by type-facts.
function narrowUnionMemberByTypeof(candidate: Type) {
return (type: Type) => {
if (isTypeSubtypeOf(type, candidate)) {
return type;
function narrowTypeByImpliedType(type: Type, candidate: Type) {
if (type.flags & TypeFlags.AnyOrUnknown) {
return candidate;
}
return mapType(type, t => {
if (isTypeRelatedTo(t, candidate, strictSubtypeRelation)) {
return t;
}
if (isTypeSubtypeOf(candidate, type)) {
return candidate;
}
if (type.flags & TypeFlags.Instantiable) {
const constraint = getBaseConstraintOfType(type) || anyType;
if (isTypeSubtypeOf(candidate, constraint)) {
return getIntersectionType([type, candidate]);
return mapType(candidate, c => {
if (!areTypesComparable(t, c)) {
return neverType;
}
}
return type;
};
if (c.flags & TypeFlags.Primitive && t.flags & TypeFlags.Object && !isEmptyAnonymousObjectType(t)) {
return isTypeSubtypeOf(c, t) ? c : neverType;
}
if (c === globalFunctionType && isTypeSubtypeOf(c, t)) {
return c;
}
return getIntersectionType([t, c]);
});
});
}
function narrowBySwitchOnTypeOf(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): Type {
@@ -25190,7 +25247,7 @@ namespace ts {
because it is caught in the first clause.
*/
const impliedType = getTypeWithFacts(getUnionType(clauseWitnesses.map(text => getImpliedTypeFromTypeofGuard(type, text) || type)), switchFacts);
return getTypeWithFacts(mapType(type, narrowUnionMemberByTypeof(impliedType)), switchFacts);
return getTypeWithFacts(narrowTypeByImpliedType(type, impliedType), switchFacts);
}
function isMatchingConstructorReference(expr: Expression) {
@@ -25523,8 +25580,10 @@ namespace ts {
!(someType(type, isGenericTypeWithoutNullableConstraint) && isGenericIndexType(getTypeOfExpression((parent as ElementAccessExpression).argumentExpression)));
}
function isGenericTypeWithUnionConstraint(type: Type) {
return !!(type.flags & TypeFlags.Instantiable && getBaseConstraintOrType(type).flags & (TypeFlags.Nullable | TypeFlags.Union));
function isGenericTypeWithUnionConstraint(type: Type): boolean {
return type.flags & TypeFlags.Intersection ?
some((type as IntersectionType).types, isGenericTypeWithUnionConstraint) :
!!(type.flags & TypeFlags.Instantiable && getBaseConstraintOrType(type).flags & (TypeFlags.Nullable | TypeFlags.Union));
}
function isGenericTypeWithoutNullableConstraint(type: Type) {
@@ -25555,7 +25614,7 @@ namespace ts {
const substituteConstraints = !(checkMode && checkMode & CheckMode.Inferential) &&
someType(type, isGenericTypeWithUnionConstraint) &&
(isConstraintPosition(type, reference) || hasContextualTypeWithNoGenericTypes(reference, checkMode));
return substituteConstraints ? mapType(type, t => t.flags & TypeFlags.Instantiable ? getBaseConstraintOrType(t) : t) : type;
return substituteConstraints ? mapType(type, getBaseConstraintOrType) : type;
}
function isExportOrExportExpression(location: Node) {
+1 -1
View File
@@ -2035,7 +2035,7 @@ namespace ts {
return comparer(a, b);
}
export function compareProperties<T, K extends keyof T>(a: T | undefined, b: T | undefined, key: K, comparer: Comparer<T[K]>): Comparison {
export function compareProperties<T extends object, K extends keyof T>(a: T | undefined, b: T | undefined, key: K, comparer: Comparer<T[K]>): Comparison {
return a === b ? Comparison.EqualTo :
a === undefined ? Comparison.LessThan :
b === undefined ? Comparison.GreaterThan :
+4
View File
@@ -1526,6 +1526,10 @@
"category": "Error",
"code": 2207
},
"This type parameter probably needs an `extends object` constraint.": {
"category": "Error",
"code": 2208
},
"The project root is ambiguous, but is required to resolve export map entry '{0}' in file '{1}'. Supply the `rootDir` compiler option to disambiguate.": {
"category": "Error",
+5
View File
@@ -5397,6 +5397,11 @@ namespace ts {
// Flags that require TypeFlags.Union
/* @internal */
ContainsIntersections = 1 << 24, // Union contains intersections
/* @internal */
IsUnknownLikeUnionComputed = 1 << 25, // IsUnknownLikeUnion flag has been computed
/* @internal */
IsUnknownLikeUnion = 1 << 26, // Union of null, undefined, and empty object type
/* @internal */
// Flags that require TypeFlags.Intersection
/* @internal */
+1 -1
View File
@@ -1564,7 +1564,7 @@ type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
/**
* Exclude null and undefined from T
*/
type NonNullable<T> = T extends null | undefined ? never : T;
type NonNullable<T> = T & {};
/**
* Obtain the parameters of a function type in a tuple
@@ -150,7 +150,7 @@ function f01(x: unknown) {
>undefined : undefined
>typeof x === "string" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {} | null
>"string" : "string"
x; // string | undefined
@@ -1,12 +1,23 @@
tests/cases/conformance/types/conditional/conditionalTypes1.ts(12,5): error TS2322: Type 'T' is not assignable to type 'NonNullable<T>'.
Type 'T' is not assignable to type '{}'.
tests/cases/conformance/types/conditional/conditionalTypes1.ts(17,5): error TS2322: Type 'T' is not assignable to type 'NonNullable<T>'.
Type 'string | undefined' is not assignable to type 'NonNullable<T>'.
Type 'undefined' is not assignable to type 'NonNullable<T>'.
Type 'undefined' is not assignable to type 'T'.
'undefined' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | undefined'.
Type 'T' is not assignable to type '{}'.
Type 'string | undefined' is not assignable to type '{}'.
Type 'undefined' is not assignable to type '{}'.
tests/cases/conformance/types/conditional/conditionalTypes1.ts(24,5): error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'NonNullable<Partial<T>[keyof T]>'.
Type 'undefined' is not assignable to type 'NonNullable<Partial<T>[keyof T]>'.
Type 'undefined' is not assignable to type 'T[keyof T] & {}'.
Type 'undefined' is not assignable to type 'T[keyof T]'.
tests/cases/conformance/types/conditional/conditionalTypes1.ts(29,5): error TS2322: Type 'T["x"]' is not assignable to type 'NonNullable<T["x"]>'.
Type 'string | undefined' is not assignable to type 'NonNullable<T["x"]>'.
Type 'undefined' is not assignable to type 'NonNullable<T["x"]>'.
Type 'undefined' is not assignable to type '{}'.
Type 'T["x"]' is not assignable to type '{}'.
Type 'string | undefined' is not assignable to type '{}'.
Type 'undefined' is not assignable to type '{}'.
tests/cases/conformance/types/conditional/conditionalTypes1.ts(103,5): error TS2322: Type 'FunctionProperties<T>' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'FunctionProperties<T>'.
tests/cases/conformance/types/conditional/conditionalTypes1.ts(104,5): error TS2322: Type 'NonFunctionProperties<T>' is not assignable to type 'T'.
@@ -72,6 +83,8 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
y = x; // Error
~
!!! error TS2322: Type 'T' is not assignable to type 'NonNullable<T>'.
!!! error TS2322: Type 'T' is not assignable to type '{}'.
!!! related TS2208 tests/cases/conformance/types/conditional/conditionalTypes1.ts:10:13: This type parameter probably needs an `extends object` constraint.
}
function f2<T extends string | undefined>(x: T, y: NonNullable<T>) {
@@ -81,6 +94,11 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
!!! error TS2322: Type 'T' is not assignable to type 'NonNullable<T>'.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'NonNullable<T>'.
!!! error TS2322: Type 'undefined' is not assignable to type 'NonNullable<T>'.
!!! error TS2322: Type 'undefined' is not assignable to type 'T'.
!!! error TS2322: 'undefined' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | undefined'.
!!! error TS2322: Type 'T' is not assignable to type '{}'.
!!! error TS2322: Type 'string | undefined' is not assignable to type '{}'.
!!! error TS2322: Type 'undefined' is not assignable to type '{}'.
let s1: string = x; // Error
let s2: string = y;
}
@@ -90,7 +108,8 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
y = x; // Error
~
!!! error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'NonNullable<Partial<T>[keyof T]>'.
!!! error TS2322: Type 'undefined' is not assignable to type 'NonNullable<Partial<T>[keyof T]>'.
!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T] & {}'.
!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'.
}
function f4<T extends { x: string | undefined }>(x: T["x"], y: NonNullable<T["x"]>) {
@@ -100,6 +119,10 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
!!! error TS2322: Type 'T["x"]' is not assignable to type 'NonNullable<T["x"]>'.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'NonNullable<T["x"]>'.
!!! error TS2322: Type 'undefined' is not assignable to type 'NonNullable<T["x"]>'.
!!! error TS2322: Type 'undefined' is not assignable to type '{}'.
!!! error TS2322: Type 'T["x"]' is not assignable to type '{}'.
!!! error TS2322: Type 'string | undefined' is not assignable to type '{}'.
!!! error TS2322: Type 'undefined' is not assignable to type '{}'.
let s1: string = x; // Error
let s2: string = y;
}
@@ -55,7 +55,7 @@ function f2<T extends string | undefined>(x: T, y: NonNullable<T>) {
let s2: string = y;
>s2 : string
>y : NonNullable<T>
>y : string
}
function f3<T>(x: Partial<T>[keyof T], y: NonNullable<Partial<T>[keyof T]>) {
@@ -96,7 +96,7 @@ function f4<T extends { x: string | undefined }>(x: T["x"], y: NonNullable<T["x"
let s2: string = y;
>s2 : string
>y : NonNullable<T["x"]>
>y : string
}
type Options = { k: "a", a: number } | { k: "b", b: string } | { k: "c", c: boolean };
@@ -32,7 +32,7 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(74,12): error TS2
Property 'bat' is missing in type 'Foo & Bar' but required in type '{ foo: string; bat: string; }'.
tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2345: Argument of type 'Extract2<T, Foo, Bar>' is not assignable to parameter of type '{ foo: string; bat: string; }'.
Type 'T extends Bar ? T : never' is not assignable to type '{ foo: string; bat: string; }'.
Property 'bat' is missing in type 'Bar & Foo' but required in type '{ foo: string; bat: string; }'.
Type 'Bar & Foo & T' is not assignable to type '{ foo: string; bat: string; }'.
==== tests/cases/conformance/types/conditional/conditionalTypes2.ts (7 errors) ====
@@ -155,8 +155,7 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2
~
!!! error TS2345: Argument of type 'Extract2<T, Foo, Bar>' is not assignable to parameter of type '{ foo: string; bat: string; }'.
!!! error TS2345: Type 'T extends Bar ? T : never' is not assignable to type '{ foo: string; bat: string; }'.
!!! error TS2345: Property 'bat' is missing in type 'Bar & Foo' but required in type '{ foo: string; bat: string; }'.
!!! related TS2728 tests/cases/conformance/types/conditional/conditionalTypes2.ts:62:43: 'bat' is declared here.
!!! error TS2345: Type 'Bar & Foo & T' is not assignable to type '{ foo: string; bat: string; }'.
}
// Repros from #22860
@@ -10,7 +10,7 @@ function f1<T extends string | undefined>(x: T, y: { a: T }, z: [T]): string {
>x : T
x;
>x : T
>x : NonNullable<T>
x.length;
>x.length : number
@@ -67,7 +67,7 @@ function f2<T>(x: Extract<T, string | undefined> | null): string {
>x : Extract<T, string | undefined> | null
x;
>x : Extract<T, string | undefined>
>x : NonNullable<Extract<T, string | undefined>>
x.length;
>x.length : number
@@ -404,11 +404,11 @@ function fx1<T, K extends keyof T>(obj: T, key: K) {
>key : K
const x2 = obj && obj[key];
>x2 : T[K]
>obj && obj[key] : T[K]
>obj : T
>obj[key] : T[K]
>x2 : NonNullable<T>[K]
>obj && obj[key] : NonNullable<T>[K]
>obj : T
>obj[key] : NonNullable<T>[K]
>obj : NonNullable<T>
>key : K
}
@@ -438,8 +438,8 @@ function fx3<T extends Record<keyof T, string> | undefined, K extends keyof T>(o
>key : K
const x1 = obj[key]; // Error
>x1 : NonNullable<Record<keyof T, string>>[K]
>obj[key] : NonNullable<Record<keyof T, string>>[K]
>x1 : Record<keyof T, string>[K]
>obj[key] : Record<keyof T, string>[K]
>obj : Record<keyof T, string> | undefined
>key : K
@@ -469,14 +469,14 @@ class TableBaseEnum<
>null : null
iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined
>iSpec[null! as keyof InternalSpec] : NonNullable<Record<keyof PublicSpec, any>>[keyof InternalSpec]
>iSpec[null! as keyof InternalSpec] : Record<keyof PublicSpec, any>[keyof InternalSpec]
>iSpec : Record<keyof PublicSpec, any> | undefined
>null! as keyof InternalSpec : keyof InternalSpec
>null! : never
>null : null
iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined
>iSpec[null! as keyof PublicSpec] : NonNullable<Record<keyof PublicSpec, any>>[keyof PublicSpec]
>iSpec[null! as keyof PublicSpec] : Record<keyof PublicSpec, any>[keyof PublicSpec]
>iSpec : Record<keyof PublicSpec, any> | undefined
>null! as keyof PublicSpec : keyof PublicSpec
>null! : never
@@ -181,7 +181,7 @@ function f8<T>(x: T) {
>x : T
x; // {}
>x : T
>x : NonNullable<T>
}
else {
x; // {}
@@ -16,7 +16,7 @@ function f1(x: unknown) {
if (typeof x === 'object') {
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {}
>'object' : "object"
obj(x);
@@ -40,7 +40,7 @@ function f2(x: unknown) {
if (typeof x === 'object') {
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {} | undefined
>'object' : "object"
obj(x);
@@ -64,7 +64,7 @@ function f3(x: unknown) {
if (typeof x === 'object') {
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {}
>'object' : "object"
obj(x);
@@ -88,7 +88,7 @@ function f4(x: unknown) {
if (typeof x === 'object') {
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {}
>'object' : "object"
obj(x);
@@ -126,7 +126,7 @@ function f5(x: unknown) {
if (typeof x === 'object') {
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {} | undefined
>'object' : "object"
obj(x);
@@ -150,12 +150,12 @@ function f6(x: unknown) {
}
else {
x;
>x : unknown
>x : {} | undefined
if (typeof x === 'object') {
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {} | undefined
>'object' : "object"
obj(x);
@@ -89,7 +89,7 @@
"containerName": "",
"fileName": "/tests/cases/fourslash/findAllRefsRootSymbols.ts",
"kind": "property",
"name": "(property) x: {} & string",
"name": "(property) x: string",
"textSpan": {
"start": 14,
"length": 1
@@ -123,26 +123,6 @@
"text": " ",
"kind": "space"
},
{
"text": "{",
"kind": "punctuation"
},
{
"text": "}",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "&",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "string",
"kind": "keyword"
@@ -258,7 +238,7 @@
"containerName": "",
"fileName": "/tests/cases/fourslash/findAllRefsRootSymbols.ts",
"kind": "property",
"name": "(property) x: {} & string",
"name": "(property) x: string",
"textSpan": {
"start": 14,
"length": 1
@@ -292,26 +272,6 @@
"text": " ",
"kind": "space"
},
{
"text": "{",
"kind": "punctuation"
},
{
"text": "}",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "&",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "string",
"kind": "keyword"
@@ -415,7 +375,7 @@
"containerName": "",
"fileName": "/tests/cases/fourslash/findAllRefsRootSymbols.ts",
"kind": "property",
"name": "(property) x: {} & string",
"name": "(property) x: string",
"textSpan": {
"start": 14,
"length": 1
@@ -449,26 +409,6 @@
"text": " ",
"kind": "space"
},
{
"text": "{",
"kind": "punctuation"
},
{
"text": "}",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "&",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "string",
"kind": "keyword"
@@ -725,7 +665,7 @@
"containerName": "",
"fileName": "/tests/cases/fourslash/findAllRefsRootSymbols.ts",
"kind": "property",
"name": "(property) x: {} & string",
"name": "(property) x: string",
"textSpan": {
"start": 14,
"length": 1
@@ -759,26 +699,6 @@
"text": " ",
"kind": "space"
},
{
"text": "{",
"kind": "punctuation"
},
{
"text": "}",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "&",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "string",
"kind": "keyword"
@@ -1,8 +1,10 @@
tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(2,5): error TS2339: Property 'toString' does not exist on type 'T'.
tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(15,6): error TS2345: Argument of type 'T' is not assignable to parameter of type '{}'.
tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(16,6): error TS2345: Argument of type 'T' is not assignable to parameter of type 'Record<string, any>'.
tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(17,5): error TS2339: Property 'toString' does not exist on type 'T'.
==== tests/cases/compiler/genericUnboundedTypeParamAssignability.ts (2 errors) ====
==== tests/cases/compiler/genericUnboundedTypeParamAssignability.ts (4 errors) ====
function f1<T>(o: T) {
o.toString(); // error
~~~~~~~~
@@ -20,7 +22,13 @@ tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(17,5): error TS23
function user<T>(t: T) {
f1(t);
f2(t); // error in strict, unbounded T doesn't satisfy the constraint
~
!!! error TS2345: Argument of type 'T' is not assignable to parameter of type '{}'.
!!! related TS2208 tests/cases/compiler/genericUnboundedTypeParamAssignability.ts:13:15: This type parameter probably needs an `extends object` constraint.
f3(t); // error in strict, unbounded T doesn't satisfy the constraint
~
!!! error TS2345: Argument of type 'T' is not assignable to parameter of type 'Record<string, any>'.
!!! related TS2208 tests/cases/compiler/genericUnboundedTypeParamAssignability.ts:13:15: This type parameter probably needs an `extends object` constraint.
t.toString(); // error, for the same reason as f1()
~~~~~~~~
!!! error TS2339: Property 'toString' does not exist on type 'T'.
@@ -3,12 +3,11 @@ tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(23,12): error TS2361: T
tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(27,12): error TS2361: The right-hand side of an 'in' expression must not be a primitive.
tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(34,12): error TS2361: The right-hand side of an 'in' expression must not be a primitive.
tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(53,14): error TS2361: The right-hand side of an 'in' expression must not be a primitive.
tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(55,18): error TS2361: The right-hand side of an 'in' expression must not be a primitive.
tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(60,12): error TS2361: The right-hand side of an 'in' expression must not be a primitive.
tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(64,12): error TS2361: The right-hand side of an 'in' expression must not be a primitive.
==== tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts (8 errors) ====
==== tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts (7 errors) ====
const validHasKey = <T extends object>(
thing: T,
key: string,
@@ -74,8 +73,6 @@ tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(64,12): error TS2361: T
!!! error TS2361: The right-hand side of an 'in' expression must not be a primitive.
if (typeof p === "object") {
"key" in p;
~
!!! error TS2361: The right-hand side of an 'in' expression must not be a primitive.
}
}
@@ -146,7 +146,7 @@ function union5<T extends object | string, U extends object | number>(p: T | U)
"key" in p;
>"key" in p : boolean
>"key" : "key"
>p : T | U
>p : (T & object) | (U & object)
}
}
@@ -346,7 +346,7 @@ function inGeneric<T extends A2 | B2>(alsoShouldBeB: T & B2) {
const b: B2 = alsoShouldBeB;
>b : B2
>alsoShouldBeB : T & B2
>alsoShouldBeB : B2
}
// Repro from #38542
@@ -374,6 +374,6 @@ function bar<T extends CA | CB>(x: T & CA) {
let ab: ABI = x;
>ab : ABI
>x : T & CA
>x : CA
}
@@ -1,8 +1,13 @@
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(7,9): error TS2322: Type 'T & U' is not assignable to type 'string | number'.
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(8,9): error TS2322: Type 'T & U' is not assignable to type 'string | null'.
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(10,9): error TS2322: Type 'T & U' is not assignable to type 'number | null'.
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(11,9): error TS2322: Type 'T & U' is not assignable to type 'number | undefined'.
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12,9): error TS2322: Type 'T & U' is not assignable to type 'null | undefined'.
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(7,9): error TS2322: Type 'string | undefined' is not assignable to type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(8,9): error TS2322: Type 'string | undefined' is not assignable to type 'string | null'.
Type 'undefined' is not assignable to type 'string | null'.
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(10,9): error TS2322: Type 'string | undefined' is not assignable to type 'number | null'.
Type 'undefined' is not assignable to type 'number | null'.
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(11,9): error TS2322: Type 'string | undefined' is not assignable to type 'number | undefined'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12,9): error TS2322: Type 'string | undefined' is not assignable to type 'null | undefined'.
Type 'string' is not assignable to type 'null | undefined'.
==== tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts (5 errors) ====
@@ -14,20 +19,25 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12
function f2<T extends string | number | undefined, U extends string | null | undefined>(x: T & U) {
let y1: string | number = x; // Error
~~
!!! error TS2322: Type 'T & U' is not assignable to type 'string | number'.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string | number'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string | number'.
let y2: string | null = x; // Error
~~
!!! error TS2322: Type 'T & U' is not assignable to type 'string | null'.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string | null'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string | null'.
let y3: string | undefined = x;
let y4: number | null = x; // Error
~~
!!! error TS2322: Type 'T & U' is not assignable to type 'number | null'.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'number | null'.
!!! error TS2322: Type 'undefined' is not assignable to type 'number | null'.
let y5: number | undefined = x; // Error
~~
!!! error TS2322: Type 'T & U' is not assignable to type 'number | undefined'.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'number | undefined'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
let y6: null | undefined = x; // Error
~~
!!! error TS2322: Type 'T & U' is not assignable to type 'null | undefined'.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'null | undefined'.
!!! error TS2322: Type 'string' is not assignable to type 'null | undefined'.
}
type T1 = (string | number | undefined) & (string | null | undefined); // string | undefined
@@ -6,7 +6,7 @@ function f1<T extends string | number, U extends string | number>(x: T & U) {
// Combined constraint of 'T & U' is 'string | number'
let y: string | number = x;
>y : string | number
>x : T & U
>x : string | number
}
function f2<T extends string | number | undefined, U extends string | null | undefined>(x: T & U) {
@@ -16,30 +16,30 @@ function f2<T extends string | number | undefined, U extends string | null | und
let y1: string | number = x; // Error
>y1 : string | number
>x : T & U
>x : string | undefined
let y2: string | null = x; // Error
>y2 : string | null
>null : null
>x : T & U
>x : string | undefined
let y3: string | undefined = x;
>y3 : string | undefined
>x : T & U
>x : string | undefined
let y4: number | null = x; // Error
>y4 : number | null
>null : null
>x : T & U
>x : string | undefined
let y5: number | undefined = x; // Error
>y5 : number | undefined
>x : T & U
>x : string | undefined
let y6: null | undefined = x; // Error
>y6 : null | undefined
>null : null
>x : T & U
>x : string | undefined
}
type T1 = (string | number | undefined) & (string | null | undefined); // string | undefined
@@ -52,7 +52,7 @@ function f3<T extends string | number | undefined>(x: T & (number | object | und
const y: number | undefined = x;
>y : number | undefined
>x : T & (number | object | undefined)
>x : number | undefined
}
function f4<T extends string | number>(x: T & (number | object)) {
@@ -61,7 +61,7 @@ function f4<T extends string | number>(x: T & (number | object)) {
const y: number = x;
>y : number
>x : T & (number | object)
>x : number
}
function f5<T, U extends keyof T>(x: keyof T & U) {
@@ -70,7 +70,7 @@ function f5<T, U extends keyof T>(x: keyof T & U) {
let y: keyof any = x;
>y : string | number | symbol
>x : keyof T & U
>x : string | number | symbol
}
// Repro from #23648
@@ -26,10 +26,10 @@ let x04: A & B & C;
>x04 : A & B
let x05: string & C;
>x05 : string & C
>x05 : string
let x06: C & string;
>x06 : C & string
>x06 : string
let x07: C;
>x07 : C
@@ -0,0 +1,677 @@
tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts(316,5): error TS2322: Type 'T' is not assignable to type '{}'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts(317,5): error TS2322: Type 'T[keyof T]' is not assignable to type '{}'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type '{}'.
Type 'T[string]' is not assignable to type '{}'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts(318,5): error TS2322: Type 'T[K]' is not assignable to type '{}'.
Type 'T[keyof T]' is not assignable to type '{}'.
==== tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts (3 errors) ====
class Shape {
name: string;
width: number;
height: number;
visible: boolean;
}
class TaggedShape extends Shape {
tag: string;
}
class Item {
name: string;
price: number;
}
class Options {
visible: "yes" | "no";
}
type Dictionary<T> = { [x: string]: T };
type NumericallyIndexed<T> = { [x: number]: T };
const enum E { A, B, C }
type K00 = keyof any; // string
type K01 = keyof string; // "toString" | "charAt" | ...
type K02 = keyof number; // "toString" | "toFixed" | "toExponential" | ...
type K03 = keyof boolean; // "valueOf"
type K04 = keyof void; // never
type K05 = keyof undefined; // never
type K06 = keyof null; // never
type K07 = keyof never; // string | number | symbol
type K08 = keyof unknown; // never
type K10 = keyof Shape; // "name" | "width" | "height" | "visible"
type K11 = keyof Shape[]; // "length" | "toString" | ...
type K12 = keyof Dictionary<Shape>; // string
type K13 = keyof {}; // never
type K14 = keyof Object; // "constructor" | "toString" | ...
type K15 = keyof E; // "toString" | "toFixed" | "toExponential" | ...
type K16 = keyof [string, number]; // "0" | "1" | "length" | "toString" | ...
type K17 = keyof (Shape | Item); // "name"
type K18 = keyof (Shape & Item); // "name" | "width" | "height" | "visible" | "price"
type K19 = keyof NumericallyIndexed<Shape> // never
type KeyOf<T> = keyof T;
type K20 = KeyOf<Shape>; // "name" | "width" | "height" | "visible"
type K21 = KeyOf<Dictionary<Shape>>; // string
type NAME = "name";
type WIDTH_OR_HEIGHT = "width" | "height";
type Q10 = Shape["name"]; // string
type Q11 = Shape["width" | "height"]; // number
type Q12 = Shape["name" | "visible"]; // string | boolean
type Q20 = Shape[NAME]; // string
type Q21 = Shape[WIDTH_OR_HEIGHT]; // number
type Q30 = [string, number][0]; // string
type Q31 = [string, number][1]; // number
type Q32 = [string, number][number]; // string | number
type Q33 = [string, number][E.A]; // string
type Q34 = [string, number][E.B]; // number
type Q35 = [string, number]["0"]; // string
type Q36 = [string, number]["1"]; // string
type Q40 = (Shape | Options)["visible"]; // boolean | "yes" | "no"
type Q41 = (Shape & Options)["visible"]; // true & "yes" | true & "no" | false & "yes" | false & "no"
type Q50 = Dictionary<Shape>["howdy"]; // Shape
type Q51 = Dictionary<Shape>[123]; // Shape
type Q52 = Dictionary<Shape>[E.B]; // Shape
declare let cond: boolean;
function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
function setProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]) {
obj[key] = value;
}
function f10(shape: Shape) {
let name = getProperty(shape, "name"); // string
let widthOrHeight = getProperty(shape, cond ? "width" : "height"); // number
let nameOrVisible = getProperty(shape, cond ? "name" : "visible"); // string | boolean
setProperty(shape, "name", "rectangle");
setProperty(shape, cond ? "width" : "height", 10);
setProperty(shape, cond ? "name" : "visible", true); // Technically not safe
}
function f11(a: Shape[]) {
let len = getProperty(a, "length"); // number
setProperty(a, "length", len);
}
function f12(t: [Shape, boolean]) {
let len = getProperty(t, "length");
let s2 = getProperty(t, "0"); // Shape
let b2 = getProperty(t, "1"); // boolean
}
function f13(foo: any, bar: any) {
let x = getProperty(foo, "x"); // any
let y = getProperty(foo, "100"); // any
let z = getProperty(foo, bar); // any
}
class Component<PropType> {
props: PropType;
getProperty<K extends keyof PropType>(key: K) {
return this.props[key];
}
setProperty<K extends keyof PropType>(key: K, value: PropType[K]) {
this.props[key] = value;
}
}
function f20(component: Component<Shape>) {
let name = component.getProperty("name"); // string
let widthOrHeight = component.getProperty(cond ? "width" : "height"); // number
let nameOrVisible = component.getProperty(cond ? "name" : "visible"); // string | boolean
component.setProperty("name", "rectangle");
component.setProperty(cond ? "width" : "height", 10)
component.setProperty(cond ? "name" : "visible", true); // Technically not safe
}
function pluck<T, K extends keyof T>(array: T[], key: K) {
return array.map(x => x[key]);
}
function f30(shapes: Shape[]) {
let names = pluck(shapes, "name"); // string[]
let widths = pluck(shapes, "width"); // number[]
let nameOrVisibles = pluck(shapes, cond ? "name" : "visible"); // (string | boolean)[]
}
function f31<K extends keyof Shape>(key: K) {
const shape: Shape = { name: "foo", width: 5, height: 10, visible: true };
return shape[key]; // Shape[K]
}
function f32<K extends "width" | "height">(key: K) {
const shape: Shape = { name: "foo", width: 5, height: 10, visible: true };
return shape[key]; // Shape[K]
}
function f33<S extends Shape, K extends keyof S>(shape: S, key: K) {
let name = getProperty(shape, "name");
let prop = getProperty(shape, key);
return prop;
}
function f34(ts: TaggedShape) {
let tag1 = f33(ts, "tag");
let tag2 = getProperty(ts, "tag");
}
class C {
public x: string;
protected y: string;
private z: string;
}
// Indexed access expressions have always permitted access to private and protected members.
// For consistency we also permit such access in indexed access types.
function f40(c: C) {
type X = C["x"];
type Y = C["y"];
type Z = C["z"];
let x: X = c["x"];
let y: Y = c["y"];
let z: Z = c["z"];
}
function f50<T>(k: keyof T, s: string) {
const x1 = s as keyof T;
const x2 = k as string;
}
function f51<T, K extends keyof T>(k: K, s: string) {
const x1 = s as keyof T;
const x2 = k as string;
}
function f52<T>(obj: { [x: string]: boolean }, k: Exclude<keyof T, symbol>, s: string, n: number) {
const x1 = obj[s];
const x2 = obj[n];
const x3 = obj[k];
}
function f53<T, K extends Exclude<keyof T, symbol>>(obj: { [x: string]: boolean }, k: K, s: string, n: number) {
const x1 = obj[s];
const x2 = obj[n];
const x3 = obj[k];
}
function f54<T>(obj: T, key: keyof T) {
for (let s in obj[key]) {
}
const b = "foo" in obj[key];
}
function f55<T, K extends keyof T>(obj: T, key: K) {
for (let s in obj[key]) {
}
const b = "foo" in obj[key];
}
function f60<T>(source: T, target: T) {
for (let k in source) {
target[k] = source[k];
}
}
function f70(func: <T, U>(k1: keyof (T | U), k2: keyof (T & U)) => void) {
func<{ a: any, b: any }, { a: any, c: any }>('a', 'a');
func<{ a: any, b: any }, { a: any, c: any }>('a', 'b');
func<{ a: any, b: any }, { a: any, c: any }>('a', 'c');
}
function f71(func: <T, U>(x: T, y: U) => Partial<T & U>) {
let x = func({ a: 1, b: "hello" }, { c: true });
x.a; // number | undefined
x.b; // string | undefined
x.c; // boolean | undefined
}
function f72(func: <T, U, K extends keyof T | keyof U>(x: T, y: U, k: K) => (T & U)[K]) {
let a = func({ a: 1, b: "hello" }, { c: true }, 'a'); // number
let b = func({ a: 1, b: "hello" }, { c: true }, 'b'); // string
let c = func({ a: 1, b: "hello" }, { c: true }, 'c'); // boolean
}
function f73(func: <T, U, K extends keyof (T & U)>(x: T, y: U, k: K) => (T & U)[K]) {
let a = func({ a: 1, b: "hello" }, { c: true }, 'a'); // number
let b = func({ a: 1, b: "hello" }, { c: true }, 'b'); // string
let c = func({ a: 1, b: "hello" }, { c: true }, 'c'); // boolean
}
function f74(func: <T, U, K extends keyof (T | U)>(x: T, y: U, k: K) => (T | U)[K]) {
let a = func({ a: 1, b: "hello" }, { a: 2, b: true }, 'a'); // number
let b = func({ a: 1, b: "hello" }, { a: 2, b: true }, 'b'); // string | boolean
}
function f80<T extends { a: { x: any } }>(obj: T) {
let a1 = obj.a; // { x: any }
let a2 = obj['a']; // { x: any }
let a3 = obj['a'] as T['a']; // T["a"]
let x1 = obj.a.x; // any
let x2 = obj['a']['x']; // any
let x3 = obj['a']['x'] as T['a']['x']; // T["a"]["x"]
}
function f81<T extends { a: { x: any } }>(obj: T) {
return obj['a']['x'] as T['a']['x'];
}
function f82() {
let x1 = f81({ a: { x: "hello" } }); // string
let x2 = f81({ a: { x: 42 } }); // number
}
function f83<T extends { [x: string]: { x: any } }, K extends keyof T>(obj: T, key: K) {
return obj[key]['x'] as T[K]['x'];
}
function f84() {
let x1 = f83({ foo: { x: "hello" } }, "foo"); // string
let x2 = f83({ bar: { x: 42 } }, "bar"); // number
}
class C1 {
x: number;
get<K extends keyof this>(key: K) {
return this[key];
}
set<K extends keyof this>(key: K, value: this[K]) {
this[key] = value;
}
foo() {
let x1 = this.x; // number
let x2 = this["x"]; // number
let x3 = this.get("x"); // this["x"]
let x4 = getProperty(this, "x"); // this["x"]
this.x = 42;
this["x"] = 42;
this.set("x", 42);
setProperty(this, "x", 42);
}
}
type S2 = {
a: string;
b: string;
};
function f90<T extends S2, K extends keyof S2>(x1: S2[keyof S2], x2: T[keyof S2], x3: S2[K]) {
x1 = x2;
x1 = x3;
x2 = x1;
x2 = x3;
x3 = x1;
x3 = x2;
x1.length;
x2.length;
x3.length;
}
function f91<T, K extends keyof T>(x: T, y: T[keyof T], z: T[K]) {
let a: {};
a = x;
~
!!! error TS2322: Type 'T' is not assignable to type '{}'.
!!! related TS2208 tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts:314:14: This type parameter probably needs an `extends object` constraint.
a = y;
~
!!! error TS2322: Type 'T[keyof T]' is not assignable to type '{}'.
!!! error TS2322: Type 'T[string] | T[number] | T[symbol]' is not assignable to type '{}'.
!!! error TS2322: Type 'T[string]' is not assignable to type '{}'.
a = z;
~
!!! error TS2322: Type 'T[K]' is not assignable to type '{}'.
!!! error TS2322: Type 'T[keyof T]' is not assignable to type '{}'.
}
function f92<T, K extends keyof T>(x: T, y: T[keyof T], z: T[K]) {
let a: {} | null | undefined;
a = x;
a = y;
a = z;
}
// Repros from #12011
class Base {
get<K extends keyof this>(prop: K) {
return this[prop];
}
set<K extends keyof this>(prop: K, value: this[K]) {
this[prop] = value;
}
}
class Person extends Base {
parts: number;
constructor(parts: number) {
super();
this.set("parts", parts);
}
getParts() {
return this.get("parts")
}
}
class OtherPerson {
parts: number;
constructor(parts: number) {
setProperty(this, "parts", parts);
}
getParts() {
return getProperty(this, "parts")
}
}
// Modified repro from #12544
function path<T, K1 extends keyof T>(obj: T, key1: K1): T[K1];
function path<T, K1 extends keyof T, K2 extends keyof T[K1]>(obj: T, key1: K1, key2: K2): T[K1][K2];
function path<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(obj: T, key1: K1, key2: K2, key3: K3): T[K1][K2][K3];
function path(obj: any, ...keys: (string | number)[]): any;
function path(obj: any, ...keys: (string | number)[]): any {
let result = obj;
for (let k of keys) {
result = result[k];
}
return result;
}
type Thing = {
a: { x: number, y: string },
b: boolean
};
function f1(thing: Thing) {
let x1 = path(thing, 'a'); // { x: number, y: string }
let x2 = path(thing, 'a', 'y'); // string
let x3 = path(thing, 'b'); // boolean
let x4 = path(thing, ...['a', 'x']); // any
}
// Repro from comment in #12114
const assignTo2 = <T, K1 extends keyof T, K2 extends keyof T[K1]>(object: T, key1: K1, key2: K2) =>
(value: T[K1][K2]) => object[key1][key2] = value;
// Modified repro from #12573
declare function one<T>(handler: (t: T) => void): T
var empty = one(() => {}) // inferred as {}, expected
type Handlers<T> = { [K in keyof T]: (t: T[K]) => void }
declare function on<T>(handlerHash: Handlers<T>): T
var hashOfEmpty1 = on({ test: () => {} }); // {}
var hashOfEmpty2 = on({ test: (x: boolean) => {} }); // { test: boolean }
// Repro from #12624
interface Options1<Data, Computed> {
data?: Data
computed?: Computed;
}
declare class Component1<Data, Computed> {
constructor(options: Options1<Data, Computed>);
get<K extends keyof (Data & Computed)>(key: K): (Data & Computed)[K];
}
let c1 = new Component1({
data: {
hello: ""
}
});
c1.get("hello");
// Repro from #12625
interface Options2<Data, Computed> {
data?: Data
computed?: Computed;
}
declare class Component2<Data, Computed> {
constructor(options: Options2<Data, Computed>);
get<K extends keyof Data | keyof Computed>(key: K): (Data & Computed)[K];
}
// Repro from #12641
interface R {
p: number;
}
function f<K extends keyof R>(p: K) {
let a: any;
a[p].add; // any
}
// Repro from #12651
type MethodDescriptor = {
name: string;
args: any[];
returnValue: any;
}
declare function dispatchMethod<M extends MethodDescriptor>(name: M['name'], args: M['args']): M['returnValue'];
type SomeMethodDescriptor = {
name: "someMethod";
args: [string, number];
returnValue: string[];
}
let result = dispatchMethod<SomeMethodDescriptor>("someMethod", ["hello", 35]);
// Repro from #13073
type KeyTypes = "a" | "b"
let MyThingy: { [key in KeyTypes]: string[] };
function addToMyThingy<S extends KeyTypes>(key: S) {
MyThingy[key].push("a");
}
// Repro from #13102
type Handler<T> = {
onChange: (name: keyof T) => void;
};
function onChangeGenericFunction<T>(handler: Handler<T & {preset: number}>) {
handler.onChange('preset')
}
// Repro from #13285
function updateIds<T extends Record<K, string>, K extends string>(
obj: T,
idFields: K[],
idMapping: Partial<Record<T[K], T[K]>>
): Record<K, string> {
for (const idField of idFields) {
const newId: T[K] | undefined = idMapping[obj[idField]];
if (newId) {
obj[idField] = newId;
}
}
return obj;
}
// Repro from #13285
function updateIds2<T extends { [x: string]: string }, K extends keyof T>(
obj: T,
key: K,
stringMap: { [oldId: string]: string }
) {
var x = obj[key];
stringMap[x]; // Should be OK.
}
// Repro from #13514
declare function head<T extends Array<any>>(list: T): T[0];
// Repro from #13604
class A<T> {
props: T & { foo: string };
}
class B extends A<{ x: number}> {
f(p: this["props"]) {
p.x;
}
}
// Repro from #13749
class Form<T> {
private childFormFactories: {[K in keyof T]: (v: T[K]) => Form<T[K]>}
public set<K extends keyof T>(prop: K, value: T[K]) {
this.childFormFactories[prop](value)
}
}
// Repro from #13787
class SampleClass<P> {
public props: Readonly<P>;
constructor(props: P) {
this.props = Object.freeze(props);
}
}
interface Foo {
foo: string;
}
declare function merge<T, U>(obj1: T, obj2: U): T & U;
class AnotherSampleClass<T> extends SampleClass<T & Foo> {
constructor(props: T) {
const foo: Foo = { foo: "bar" };
super(merge(props, foo));
}
public brokenMethod() {
this.props.foo.concat;
}
}
new AnotherSampleClass({});
// Positive repro from #17166
function f3<T, K extends Extract<keyof T, string>>(t: T, k: K, tk: T[K]): void {
for (let key in t) {
key = k // ok, K ==> keyof T
t[key] = tk; // ok, T[K] ==> T[keyof T]
}
}
// # 21185
type Predicates<TaggedRecord> = {
[T in keyof TaggedRecord]: (variant: TaggedRecord[keyof TaggedRecord]) => variant is TaggedRecord[T]
}
// Repros from #23592
type Example<T extends { [K in keyof T]: { prop: any } }> = { [K in keyof T]: T[K]["prop"] };
type Result = Example<{ a: { prop: string }; b: { prop: number } }>;
type Helper2<T> = { [K in keyof T]: Extract<T[K], { prop: any }> };
type Example2<T> = { [K in keyof Helper2<T>]: Helper2<T>[K]["prop"] };
type Result2 = Example2<{ 1: { prop: string }; 2: { prop: number } }>;
// Repro from #23618
type DBBoolTable<K extends string> = { [k in K]: 0 | 1 }
enum Flag {
FLAG_1 = "flag_1",
FLAG_2 = "flag_2"
}
type SimpleDBRecord<Flag extends string> = { staticField: number } & DBBoolTable<Flag>
function getFlagsFromSimpleRecord<Flag extends string>(record: SimpleDBRecord<Flag>, flags: Flag[]) {
return record[flags[0]];
}
type DynamicDBRecord<Flag extends string> = ({ dynamicField: number } | { dynamicField: string }) & DBBoolTable<Flag>
function getFlagsFromDynamicRecord<Flag extends string>(record: DynamicDBRecord<Flag>, flags: Flag[]) {
return record[flags[0]];
}
// Repro from #21368
interface I {
foo: string;
}
declare function take<T>(p: T): void;
function fn<T extends I, K extends keyof T>(o: T, k: K) {
take<{} | null | undefined>(o[k]);
take<any>(o[k]);
}
// Repro from #23133
class Unbounded<T> {
foo(x: T[keyof T]) {
let y: {} | undefined | null = x;
}
}
// Repro from #23940
interface I7 {
x: any;
}
type Foo7<T extends number> = T;
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;
// Repro from #21770
type Dict<T extends string> = { [key in T]: number };
type DictDict<V extends string, T extends string> = { [key in V]: Dict<T> };
function ff1<V extends string, T extends string>(dd: DictDict<V, T>, k1: V, k2: T): number {
return dd[k1][k2];
}
function ff2<V extends string, T extends string>(dd: DictDict<V, T>, k1: V, k2: T): number {
const d: Dict<T> = dd[k1];
return d[k2];
}
// Repro from #26409
const cf1 = <T extends { [P in K]: string; } & { cool: string; }, K extends keyof T>(t: T, k: K) =>
{
const s: string = t[k];
t.cool;
};
const cf2 = <T extends { [P in K | "cool"]: string; }, K extends keyof T>(t: T, k: K) =>
{
const s: string = t[k];
t.cool;
};
@@ -1,9 +1,10 @@
tests/cases/conformance/types/mapped/mappedTypeAsClauseRelationships.ts(12,9): error TS2322: Type 'T' is not assignable to type 'Modify<T>'.
tests/cases/conformance/types/mapped/mappedTypeAsClauseRelationships.ts(22,9): error TS2322: Type 'T' is not assignable to type 'ModifyInclOpt<T>'.
tests/cases/conformance/types/mapped/mappedTypeAsClauseRelationships.ts(23,9): error TS2322: Type 'T' is not assignable to type 'FilterExclOpt<T>'.
tests/cases/conformance/types/mapped/mappedTypeAsClauseRelationships.ts(24,9): error TS2322: Type 'T' is not assignable to type 'ModifyExclOpt<T>'.
==== tests/cases/conformance/types/mapped/mappedTypeAsClauseRelationships.ts (3 errors) ====
==== tests/cases/conformance/types/mapped/mappedTypeAsClauseRelationships.ts (4 errors) ====
// From original issue #45212:
type Methods<T> = { [P in keyof T as T[P] extends Function ? P : never]: T[P] };
type H<T> = T[keyof Methods<T>]; // Ok
@@ -28,6 +29,8 @@ tests/cases/conformance/types/mapped/mappedTypeAsClauseRelationships.ts(24,9): e
function fun2<T>(val: T) {
let x: FilterInclOpt<T> = val; // Ok
let y: ModifyInclOpt<T> = val; // Ok
~
!!! error TS2322: Type 'T' is not assignable to type 'ModifyInclOpt<T>'.
let z: FilterExclOpt<T> = val; // Error
~
!!! error TS2322: Type 'T' is not assignable to type 'FilterExclOpt<T>'.
+6 -6
View File
@@ -27,17 +27,17 @@ function boxify<T>(obj: T): Boxified<T> {
for (let k in obj) {
>k : Extract<keyof T, string>
>obj : T
>obj : (T & null) | (T & object)
result[k] = { value: obj[k] };
>result[k] = { value: obj[k] } : { value: T[Extract<keyof T, string>]; }
>result[k] = { value: obj[k] } : { value: NonNullable<T & object>[Extract<keyof T, string>]; }
>result[k] : Boxified<T>[Extract<keyof T, string>]
>result : Boxified<T>
>k : Extract<keyof T, string>
>{ value: obj[k] } : { value: T[Extract<keyof T, string>]; }
>value : T[Extract<keyof T, string>]
>obj[k] : T[Extract<keyof T, string>]
>obj : T
>{ value: obj[k] } : { value: NonNullable<T & object>[Extract<keyof T, string>]; }
>value : NonNullable<T & object>[Extract<keyof T, string>]
>obj[k] : NonNullable<T & object>[Extract<keyof T, string>]
>obj : NonNullable<T & object>
>k : Extract<keyof T, string>
}
return result;
@@ -7,6 +7,20 @@ tests/cases/conformance/types/mapped/mappedTypes6.ts(37,5): error TS2322: Type '
Type 'T[keyof T]' is not assignable to type 'NonNullable<T[P]>'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'NonNullable<T[P]>'.
Type 'T[string]' is not assignable to type 'NonNullable<T[P]>'.
Type 'T[string]' is not assignable to type 'T[P]'.
Type 'string' is not assignable to type 'P'.
'string' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'string | number | symbol'.
Type 'T[keyof T]' is not assignable to type 'T[P]'.
Type 'keyof T' is not assignable to type 'P'.
'keyof T' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'string | number | symbol'.
Type 'string | number | symbol' is not assignable to type 'P'.
'string | number | symbol' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'string | number | symbol'.
Type 'string' is not assignable to type 'P'.
'string' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'string | number | symbol'.
Type 'T[P]' is not assignable to type '{}'.
Type 'T[keyof T]' is not assignable to type '{}'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type '{}'.
Type 'T[string]' is not assignable to type '{}'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(38,5): error TS2322: Type 'T' is not assignable to type 'Denullified<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(39,5): error TS2322: Type 'Partial<T>' is not assignable to type 'Denullified<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(42,5): error TS2322: Type 'T' is not assignable to type 'Required<T>'.
@@ -77,6 +91,20 @@ tests/cases/conformance/types/mapped/mappedTypes6.ts(120,4): error TS2540: Canno
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'NonNullable<T[P]>'.
!!! error TS2322: Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'NonNullable<T[P]>'.
!!! error TS2322: Type 'T[string]' is not assignable to type 'NonNullable<T[P]>'.
!!! error TS2322: Type 'T[string]' is not assignable to type 'T[P]'.
!!! error TS2322: Type 'string' is not assignable to type 'P'.
!!! error TS2322: 'string' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'string | number | symbol'.
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'T[P]'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'P'.
!!! error TS2322: 'keyof T' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'string | number | symbol'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'P'.
!!! error TS2322: 'string | number | symbol' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'string | number | symbol'.
!!! error TS2322: Type 'string' is not assignable to type 'P'.
!!! error TS2322: 'string' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'string | number | symbol'.
!!! error TS2322: Type 'T[P]' is not assignable to type '{}'.
!!! error TS2322: Type 'T[keyof T]' is not assignable to type '{}'.
!!! error TS2322: Type 'T[string] | T[number] | T[symbol]' is not assignable to type '{}'.
!!! error TS2322: Type 'T[string]' is not assignable to type '{}'.
w = y; // Error
~
!!! error TS2322: Type 'T' is not assignable to type 'Denullified<T>'.
@@ -0,0 +1,54 @@
tests/cases/conformance/types/mapped/mappedTypesAndObjects.ts(25,11): error TS2430: Interface 'E1<T>' incorrectly extends interface 'Base'.
Types of property 'foo' are incompatible.
Type 'T' is not assignable to type '{ [key: string]: any; }'.
==== tests/cases/conformance/types/mapped/mappedTypesAndObjects.ts (1 errors) ====
function f1<T>(x: Partial<T>, y: Readonly<T>) {
let obj: {};
obj = x;
obj = y;
}
function f2<T>(x: Partial<T>, y: Readonly<T>) {
let obj: { [x: string]: any };
obj = x;
obj = y;
}
function f3<T>(x: Partial<T>) {
x = {};
}
// Repro from #12900
interface Base {
foo: { [key: string]: any };
bar: any;
baz: any;
}
interface E1<T> extends Base {
~~
!!! error TS2430: Interface 'E1<T>' incorrectly extends interface 'Base'.
!!! error TS2430: Types of property 'foo' are incompatible.
!!! error TS2430: Type 'T' is not assignable to type '{ [key: string]: any; }'.
!!! related TS2208 tests/cases/conformance/types/mapped/mappedTypesAndObjects.ts:25:14: This type parameter probably needs an `extends object` constraint.
foo: T;
}
interface Something { name: string, value: string };
interface E2 extends Base {
foo: Partial<Something>; // or other mapped type
}
interface E3<T> extends Base {
foo: Partial<T>; // or other mapped type
}
// Repro from #13747
class Form<T> {
private values: {[P in keyof T]?: T[P]} = {}
}
@@ -144,7 +144,7 @@ if (xUnknown == null) {
} else {
xUnknown
>xUnknown : unknown
>xUnknown : {}
}
if (xUnknown != null) {
@@ -153,7 +153,7 @@ if (xUnknown != null) {
>null : null
xUnknown;
>xUnknown : unknown
>xUnknown : {}
} else {
xUnknown;
@@ -552,14 +552,14 @@ function multipleGenericFuse<X extends L | number, Y extends R | number>(xy: X |
case 'function': return [xy, 1];
>'function' : "function"
>[xy, 1] : [X, 1]
>xy : X
>[xy, 1] : [X & Function, 1]
>xy : X & Function
>1 : 1
case 'object': return [xy, 'two'];
>'object' : "object"
>[xy, 'two'] : [Y, string]
>xy : Y
>[xy, 'two'] : [Y & object, string]
>xy : Y & object
>'two' : "two"
case 'number': return [xy]
@@ -843,7 +843,7 @@ function narrowingNarrows(x: {} | undefined) {
case 'object': const _: {} = x; return;
>'object' : "object"
>_ : {}
>x : {}
>x : object
case 'string': assertString(x); return;
>'string' : "string"
@@ -1092,14 +1092,14 @@ function multipleGenericFuseWithBoth<X extends L | number, Y extends R | number>
case `function`: return [xy, 1];
>`function` : "function"
>[xy, 1] : [X, 1]
>xy : X
>[xy, 1] : [X & Function, 1]
>xy : X & Function
>1 : 1
case 'object': return [xy, 'two'];
>'object' : "object"
>[xy, 'two'] : [Y, string]
>xy : Y
>[xy, 'two'] : [Y & object, string]
>xy : Y & object
>'two' : "two"
case `number`: return [xy]
@@ -35,7 +35,7 @@ function foo(x: unknown, b: boolean) {
>x : unknown
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {}
>'object' : "object"
x.toString();
@@ -51,7 +51,7 @@ function foo(x: unknown, b: boolean) {
>x : unknown
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {}
>'object' : "object"
x.toString();
@@ -67,7 +67,7 @@ function foo(x: unknown, b: boolean) {
>b : boolean
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {}
>'object' : "object"
x.toString();
@@ -85,7 +85,7 @@ function foo(x: unknown, b: boolean) {
>b : true
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {}
>'object' : "object"
x.toString();
@@ -107,7 +107,7 @@ function foo(x: unknown, b: boolean) {
>b : true
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {}
>'object' : "object"
x.toString();
@@ -130,7 +130,7 @@ function f1(x: unknown): any {
>x : unknown
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {}
>'object' : "object"
>x.hasOwnProperty('x') : boolean
>x.hasOwnProperty : (v: PropertyKey) => boolean
@@ -35,7 +35,7 @@ function f2<T>(x: (T & F) | T & string) {
>"function" : "function"
x;
>x : (T & F) | (T & string)
>x : T & F
}
else {
x;
@@ -1,6 +1,5 @@
tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(1,5): error TS2322: Type 'string' is not assignable to type 'never'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(3,5): error TS2322: Type 'number' is not assignable to type 'object & {}'.
Type 'number' is not assignable to type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(3,5): error TS2322: Type 'number' is not assignable to type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(4,1): error TS2322: Type 'string' is not assignable to type 'never'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(8,38): error TS2322: Type '{ bar: string; }' is not assignable to type 'object & { err: string; }'.
Object literal may only specify known properties, and 'bar' does not exist in type 'object & { err: string; }'.
@@ -13,8 +12,7 @@ tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(8,38
var b: object | string = ""; // ok
var c: object & {} = 123; // error
~
!!! error TS2322: Type 'number' is not assignable to type 'object & {}'.
!!! error TS2322: Type 'number' is not assignable to type 'object'.
!!! error TS2322: Type 'number' is not assignable to type 'object'.
a = b; // error
~
!!! error TS2322: Type 'string' is not assignable to type 'never'.
@@ -8,7 +8,7 @@ var b: object | string = ""; // ok
>"" : ""
var c: object & {} = 123; // error
>c : object & {}
>c : object
>123 : 123
a = b; // error
@@ -22,7 +22,7 @@ b = a; // ok
>a : never
const foo: object & {} = {bar: 'bar'}; // ok
>foo : object & {}
>foo : object
>{bar: 'bar'} : { bar: string; }
>bar : string
>'bar' : "bar"
@@ -179,7 +179,7 @@ const arrayOfTypes = [PropTypes.string, PropTypes.bool, PropTypes.shape({
// TS checking
const propTypes: PropTypesMap = {
>propTypes : PropTypes.ValidationMap<Props>
>{ any: PropTypes.any, array: PropTypes.array.isRequired, bool: PropTypes.bool.isRequired, shape: PropTypes.shape(innerProps).isRequired, oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired,} : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>; oneOfType: PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>; }
>{ any: PropTypes.any, array: PropTypes.array.isRequired, bool: PropTypes.bool.isRequired, shape: PropTypes.shape(innerProps).isRequired, oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired,} : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }
any: PropTypes.any,
>any : PropTypes.Requireable<any>
@@ -204,31 +204,31 @@ const propTypes: PropTypesMap = {
>isRequired : PropTypes.Validator<boolean>
shape: PropTypes.shape(innerProps).isRequired,
>shape : PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>
>PropTypes.shape(innerProps).isRequired : PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>
>shape : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>
>PropTypes.shape(innerProps).isRequired : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>
>PropTypes.shape(innerProps) : PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>
>PropTypes.shape : <P extends PropTypes.ValidationMap<any>>(type: P) => PropTypes.Requireable<PropTypes.InferProps<P>>
>PropTypes : typeof PropTypes
>shape : <P extends PropTypes.ValidationMap<any>>(type: P) => PropTypes.Requireable<PropTypes.InferProps<P>>
>innerProps : { foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }
>isRequired : PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>
>isRequired : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>
oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired,
>oneOfType : PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>
>PropTypes.oneOfType(arrayOfTypes).isRequired : PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>
>PropTypes.oneOfType(arrayOfTypes) : PropTypes.Requireable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>
>oneOfType : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>
>PropTypes.oneOfType(arrayOfTypes).isRequired : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>
>PropTypes.oneOfType(arrayOfTypes) : PropTypes.Requireable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>
>PropTypes.oneOfType : <T extends PropTypes.Validator<any>>(types: T[]) => PropTypes.Requireable<NonNullable<PropTypes.InferType<T>>>
>PropTypes : typeof PropTypes
>oneOfType : <T extends PropTypes.Validator<any>>(types: T[]) => PropTypes.Requireable<NonNullable<PropTypes.InferType<T>>>
>arrayOfTypes : (PropTypes.Requireable<boolean> | PropTypes.Requireable<string> | PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>)[]
>isRequired : PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>
>isRequired : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>
};
// JS checking
const propTypesWithoutAnnotation = {
>propTypesWithoutAnnotation : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>; oneOfType: PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>; }
>{ any: PropTypes.any, array: PropTypes.array.isRequired, bool: PropTypes.bool.isRequired, shape: PropTypes.shape(innerProps).isRequired, oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired,} : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>; oneOfType: PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>; }
>propTypesWithoutAnnotation : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }
>{ any: PropTypes.any, array: PropTypes.array.isRequired, bool: PropTypes.bool.isRequired, shape: PropTypes.shape(innerProps).isRequired, oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired,} : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }
any: PropTypes.any,
>any : PropTypes.Requireable<any>
@@ -253,24 +253,24 @@ const propTypesWithoutAnnotation = {
>isRequired : PropTypes.Validator<boolean>
shape: PropTypes.shape(innerProps).isRequired,
>shape : PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>
>PropTypes.shape(innerProps).isRequired : PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>
>shape : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>
>PropTypes.shape(innerProps).isRequired : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>
>PropTypes.shape(innerProps) : PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>
>PropTypes.shape : <P extends PropTypes.ValidationMap<any>>(type: P) => PropTypes.Requireable<PropTypes.InferProps<P>>
>PropTypes : typeof PropTypes
>shape : <P extends PropTypes.ValidationMap<any>>(type: P) => PropTypes.Requireable<PropTypes.InferProps<P>>
>innerProps : { foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }
>isRequired : PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>
>isRequired : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>
oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired,
>oneOfType : PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>
>PropTypes.oneOfType(arrayOfTypes).isRequired : PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>
>PropTypes.oneOfType(arrayOfTypes) : PropTypes.Requireable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>
>oneOfType : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>
>PropTypes.oneOfType(arrayOfTypes).isRequired : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>
>PropTypes.oneOfType(arrayOfTypes) : PropTypes.Requireable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>
>PropTypes.oneOfType : <T extends PropTypes.Validator<any>>(types: T[]) => PropTypes.Requireable<NonNullable<PropTypes.InferType<T>>>
>PropTypes : typeof PropTypes
>oneOfType : <T extends PropTypes.Validator<any>>(types: T[]) => PropTypes.Requireable<NonNullable<PropTypes.InferType<T>>>
>arrayOfTypes : (PropTypes.Requireable<boolean> | PropTypes.Requireable<string> | PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>)[]
>isRequired : PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>
>isRequired : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>
};
@@ -280,9 +280,9 @@ type ExtractedProps = PropTypes.InferProps<typeof propTypes>;
>propTypes : PropTypes.ValidationMap<Props>
type ExtractedPropsWithoutAnnotation = PropTypes.InferProps<typeof propTypesWithoutAnnotation>;
>ExtractedPropsWithoutAnnotation : PropTypes.InferPropsInner<Pick<{ any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>; oneOfType: PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>; }, PropTypes.RequiredKeys<{ any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>; oneOfType: PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>; }>>> & Partial<PropTypes.InferPropsInner<Pick<{ any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>; oneOfType: PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>; }, "any">>>
>ExtractedPropsWithoutAnnotation : PropTypes.InferPropsInner<Pick<{ any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }, PropTypes.RequiredKeys<{ any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }>>> & Partial<PropTypes.InferPropsInner<Pick<{ any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }, "any">>>
>PropTypes : any
>propTypesWithoutAnnotation : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>; oneOfType: PropTypes.Validator<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>; }
>propTypesWithoutAnnotation : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }
type ExtractPropsMatch = ExtractedProps extends ExtractedPropsWithoutAnnotation ? true : false;
>ExtractPropsMatch : true
@@ -1,7 +1,7 @@
tests/cases/compiler/recursiveTupleTypeInference.ts(23,5): error TS2345: Argument of type '{ b: A; }' is not assignable to parameter of type 'G<{ b: unknown; }>'.
Types of property 'b' are incompatible.
Type 'A' is not assignable to type '[[[[[[[[[[[[any, "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"]'.
Type 'string' is not assignable to type '[[[[[[[[[[[[any, "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"]'.
Type 'A' is not assignable to type '[never, "null"]'.
Type 'string' is not assignable to type '[never, "null"]'.
==== tests/cases/compiler/recursiveTupleTypeInference.ts (1 errors) ====
@@ -31,6 +31,6 @@ tests/cases/compiler/recursiveTupleTypeInference.ts(23,5): error TS2345: Argumen
~~
!!! error TS2345: Argument of type '{ b: A; }' is not assignable to parameter of type 'G<{ b: unknown; }>'.
!!! error TS2345: Types of property 'b' are incompatible.
!!! error TS2345: Type 'A' is not assignable to type '[[[[[[[[[[[[any, "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"]'.
!!! error TS2345: Type 'string' is not assignable to type '[[[[[[[[[[[[any, "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"], "null"]'.
!!! error TS2345: Type 'A' is not assignable to type '[never, "null"]'.
!!! error TS2345: Type 'string' is not assignable to type '[never, "null"]'.
@@ -111,7 +111,7 @@ declare function f5<S, T extends undefined>(a: S | T): S | T;
declare function f6<T extends object | undefined>(a: T): T;
declare function g1<T extends {}, A extends {
z: (T | undefined) & T;
}>(a: A): (T | undefined) & T;
}>(a: A): T | (undefined & T);
interface DatafulFoo<T> {
data: T;
}
@@ -56,19 +56,19 @@ function f6<T extends object | undefined>(a: T) {
// Repro from #46976
function g1<T extends {}, A extends { z: (T | undefined) & T }>(a: A) {
>g1 : <T extends {}, A extends { z: (T | undefined) & T; }>(a: A) => (T | undefined) & T
>z : (T | undefined) & T
>g1 : <T extends {}, A extends { z: (T | undefined) & T; }>(a: A) => T | (undefined & T)
>z : T | (undefined & T)
>a : A
const { z } = a;
>z : (T | undefined) & T
>z : T | (undefined & T)
>a : A
return {
>{ ...z } : (T | undefined) & T
>{ ...z } : T | (undefined & T)
...z
>z : (T | undefined) & T
>z : T | (undefined & T)
};
}
@@ -98,9 +98,9 @@ class Foo<T extends string> {
this.data.toLocaleLowerCase();
>this.data.toLocaleLowerCase() : string
>this.data.toLocaleLowerCase : (locales?: string | string[] | undefined) => string
>this.data : (T | undefined) & T
>this.data : T | (undefined & T)
>this : this & DatafulFoo<T>
>data : (T | undefined) & T
>data : T | (undefined & T)
>toLocaleLowerCase : (locales?: string | string[] | undefined) => string
}
}
@@ -6,7 +6,6 @@ error TS2318: Cannot find global type 'Number'.
error TS2318: Cannot find global type 'Object'.
error TS2318: Cannot find global type 'RegExp'.
error TS2318: Cannot find global type 'String'.
tests/cases/compiler/strictNullNotNullIndexTypeNoLib.ts(10,28): error TS2339: Property 'name' does not exist on type 'T["params"]'.
!!! error TS2318: Cannot find global type 'Array'.
@@ -17,7 +16,7 @@ tests/cases/compiler/strictNullNotNullIndexTypeNoLib.ts(10,28): error TS2339: Pr
!!! error TS2318: Cannot find global type 'Object'.
!!! error TS2318: Cannot find global type 'RegExp'.
!!! error TS2318: Cannot find global type 'String'.
==== tests/cases/compiler/strictNullNotNullIndexTypeNoLib.ts (1 errors) ====
==== tests/cases/compiler/strictNullNotNullIndexTypeNoLib.ts (0 errors) ====
type Readonly<T> = {readonly [K in keyof T]: T[K]}
interface A {
params?: { name: string; };
@@ -28,8 +27,6 @@ tests/cases/compiler/strictNullNotNullIndexTypeNoLib.ts(10,28): error TS2339: Pr
m() {
this.attrs.params!.name;
~~~~
!!! error TS2339: Property 'name' does not exist on type 'T["params"]'.
}
}
@@ -29,11 +29,13 @@ class Test<T extends A> {
>m : Symbol(Test.m, Decl(strictNullNotNullIndexTypeNoLib.ts, 6, 23))
this.attrs.params!.name;
>this.attrs.params!.name : Symbol(name, Decl(strictNullNotNullIndexTypeNoLib.ts, 2, 14))
>this.attrs.params : Symbol(params, Decl(strictNullNotNullIndexTypeNoLib.ts, 1, 13))
>this.attrs : Symbol(Test.attrs, Decl(strictNullNotNullIndexTypeNoLib.ts, 5, 25))
>this : Symbol(Test, Decl(strictNullNotNullIndexTypeNoLib.ts, 3, 1))
>attrs : Symbol(Test.attrs, Decl(strictNullNotNullIndexTypeNoLib.ts, 5, 25))
>params : Symbol(params, Decl(strictNullNotNullIndexTypeNoLib.ts, 1, 13))
>name : Symbol(name, Decl(strictNullNotNullIndexTypeNoLib.ts, 2, 14))
}
}
@@ -18,14 +18,14 @@ class Test<T extends A> {
>m : () => void
this.attrs.params!.name;
>this.attrs.params!.name : any
>this.attrs.params! : T["params"]
>this.attrs.params!.name : string
>this.attrs.params! : T["params"] & {}
>this.attrs.params : T["params"] | undefined
>this.attrs : Readonly<T>
>this : this
>attrs : Readonly<T>
>params : T["params"] | undefined
>name : any
>name : string
}
}
@@ -62,10 +62,10 @@ class Test2<T extends A> {
>attrs : Readonly<T>
m() {
>m : () => T["params"]
>m : () => T["params"] & {}
return this.attrs.params!; // Return type should maintain relationship with `T` after being not-null-asserted, ideally
>this.attrs.params! : T["params"]
>this.attrs.params! : T["params"] & {}
>this.attrs.params : T["params"] | undefined
>this.attrs : Readonly<T>
>this : this
@@ -653,7 +653,7 @@ function expectNotUndefined<T>(value: Undefinable<T>): T {
>'value is undefined' : "value is undefined"
}
return value;
>value : T
>value : T & ({} | null)
}
interface Bar {
@@ -54,7 +54,7 @@ type E = `${A & {}}`;
>E : "a"
type MixE = E & {}
>MixE : "a" & {}
>MixE : "a"
type OriginE = `${MixE}`
>OriginE : "a"
@@ -5,9 +5,17 @@ tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx(15,14): error TS2769: No o
Type '{}' is not assignable to type 'Readonly<P>'.
Overload 2 of 2, '(props: P, context?: any): MyComponent', gave the following error.
Type '{}' is not assignable to type 'Readonly<P>'.
tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx(17,14): error TS2322: Type 'P' is not assignable to type 'IntrinsicAttributes & P'.
Type 'P' is not assignable to type 'IntrinsicAttributes'.
tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx(18,14): error TS2769: No overload matches this call.
Overload 1 of 2, '(props: Readonly<P>): MyComponent', gave the following error.
Type 'P' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<MyComponent> & Readonly<{ children?: ReactNode; }> & Readonly<P>'.
Type 'P' is not assignable to type 'IntrinsicAttributes'.
Overload 2 of 2, '(props: P, context?: any): MyComponent', gave the following error.
Type 'P' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<MyComponent> & Readonly<{ children?: ReactNode; }> & Readonly<P>'.
==== tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx (2 errors) ====
==== tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx (4 errors) ====
/// <reference path="/.lib/react16.d.ts" />
import React from 'react';
@@ -34,5 +42,17 @@ tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx(15,14): error TS2769: No o
!!! error TS2769: Type '{}' is not assignable to type 'Readonly<P>'.
let z = <MySFC {...wrappedProps} /> // should work
~~~~~
!!! error TS2322: Type 'P' is not assignable to type 'IntrinsicAttributes & P'.
!!! error TS2322: Type 'P' is not assignable to type 'IntrinsicAttributes'.
!!! related TS2208 tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx:5:15: This type parameter probably needs an `extends object` constraint.
let q = <MyComponent {...wrappedProps} /> // should work
~~~~~~~~~~~
!!! error TS2769: No overload matches this call.
!!! error TS2769: Overload 1 of 2, '(props: Readonly<P>): MyComponent', gave the following error.
!!! error TS2769: Type 'P' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<MyComponent> & Readonly<{ children?: ReactNode; }> & Readonly<P>'.
!!! error TS2769: Type 'P' is not assignable to type 'IntrinsicAttributes'.
!!! error TS2769: Overload 2 of 2, '(props: P, context?: any): MyComponent', gave the following error.
!!! error TS2769: Type 'P' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<MyComponent> & Readonly<{ children?: ReactNode; }> & Readonly<P>'.
!!! related TS2208 tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx:5:15: This type parameter probably needs an `extends object` constraint.
}
@@ -242,7 +242,7 @@ function test9(a: boolean | number) {
}
else {
a;
>a : undefined
>a : never
}
}
@@ -259,7 +259,7 @@ function test10(a: boolean | number) {
if (typeof a === "boolean") {
>typeof a === "boolean" : boolean
>typeof a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>a : undefined
>a : never
>"boolean" : "boolean"
a;
@@ -267,7 +267,7 @@ function test10(a: boolean | number) {
}
else {
a;
>a : undefined
>a : never
}
}
else {
@@ -193,8 +193,8 @@ function f5<T, K extends keyof T>(obj: T | undefined, key: K) {
>obj : T | undefined
obj[key];
>obj[key] : T[K]
>obj : T
>obj[key] : NonNullable<T>[K]
>obj : NonNullable<T>
>key : K
}
}
@@ -21,7 +21,7 @@ export function foo<T extends NumList | StrList>(arr: T & (NumList | StrList)) {
let zz = arr[1]; // Error
>zz : string | number
>arr[1] : string | number
>arr : T & (NumList | StrList)
>arr : NumList | StrList
>1 : 1
}
@@ -0,0 +1,266 @@
tests/cases/conformance/types/unknown/unknownControlFlow.ts(18,9): error TS2322: Type 'unknown' is not assignable to type '{}'.
==== tests/cases/conformance/types/unknown/unknownControlFlow.ts (1 errors) ====
type T01 = {} & string; // string
type T02 = {} & 'a'; // 'a'
type T03 = {} & object; // object
type T04 = {} & { x: number }; // { x: number }
type T05 = {} & null; // never
type T06 = {} & undefined; // never
type T07 = undefined & void; // undefined
type T10 = string & {}; // Specially preserved
type T11 = number & {}; // Specially preserved
type T12 = bigint & {}; // Specially preserved
type ThisNode = {};
type ThatNode = {};
type ThisOrThatNode = ThisNode | ThatNode;
function f01(u: unknown) {
let x1: {} = u; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type '{}'.
let x2: {} | null | undefined = u;
let x3: {} | { x: string } | null | undefined = u;
let x4: ThisOrThatNode | null | undefined = u;
}
function f10(x: unknown) {
if (x) {
x; // {}
}
else {
x; // unknown
}
if (!x) {
x; // unknown
}
else {
x; // {}
}
}
function f11<T>(x: T) {
if (x) {
x; // T & {}
}
else {
x; // T
}
if (!x) {
x; // T
}
else {
x; // T & {}
}
}
function f12<T extends {}>(x: T) {
if (x) {
x; // T
}
else {
x; // T
}
}
function f20(x: unknown) {
if (x !== undefined) {
x; // {} | null
}
else {
x; // undefined
}
if (x !== null) {
x; // {} | undefined
}
else {
x; // null
}
if (x !== undefined && x !== null) {
x; // {}
}
else {
x; // null | undefined
}
if (x != undefined) {
x; // {}
}
else {
x; // null | undefined
}
if (x != null) {
x; // {}
}
else {
x; // null | undefined
}
}
function f21<T>(x: T) {
if (x !== undefined) {
x; // T & ({} | null)
}
else {
x; // T
}
if (x !== null) {
x; // T & ({} | undefined)
}
else {
x; // T
}
if (x !== undefined && x !== null) {
x; // T & {}
}
else {
x; // T
}
if (x != undefined) {
x; // T & {}
}
else {
x; // T
}
if (x != null) {
x; // T & {}
}
else {
x; // T
}
}
function f22<T extends {} | undefined>(x: T) {
if (x !== undefined) {
x; // T & {}
}
else {
x; // T
}
if (x !== null) {
x; // T
}
else {
x; // T
}
if (x !== undefined && x !== null) {
x; // T & {}
}
else {
x; // T
}
if (x != undefined) {
x; // T & {}
}
else {
x; // T
}
if (x != null) {
x; // T & {}
}
else {
x; // T
}
}
function f23<T>(x: T | undefined | null) {
if (x !== undefined) {
x; // T & {} | null
}
if (x !== null) {
x; // T & {} | undefined
}
if (x != undefined) {
x; // T & {}
}
if (x != null) {
x; // T & {}
}
}
function f30(x: {}) {
if (typeof x === "object") {
x; // object
}
}
function f31<T>(x: T) {
if (typeof x === "object") {
x; // T & object | T & null
}
if (x && typeof x === "object") {
x; // T & object
}
if (typeof x === "object" && x) {
x; // T & object
}
}
function f32<T extends {} | undefined>(x: T) {
if (typeof x === "object") {
x; // T & object
}
}
function possiblyNull<T>(x: T) {
return !!true ? x : null; // T | null
}
function possiblyUndefined<T>(x: T) {
return !!true ? x : undefined; // T | undefined
}
function possiblyNullOrUndefined<T>(x: T) {
return possiblyUndefined(possiblyNull(x)); // T | null | undefined
}
function ensureNotNull<T>(x: T) {
if (x === null) throw Error();
return x; // T & ({} | undefined)
}
function ensureNotUndefined<T>(x: T) {
if (x === undefined) throw Error();
return x; // T & ({} | null)
}
function ensureNotNullOrUndefined<T>(x: T) {
return ensureNotUndefined(ensureNotNull(x)); // T & {}
}
function f40(a: string | undefined, b: number | null | undefined) {
let a1 = ensureNotNullOrUndefined(a); // string
let b1 = ensureNotNullOrUndefined(b); // number
}
type QQ<T> = NonNullable<NonNullable<NonNullable<T>>>;
function f41<T>(a: T) {
let a1 = ensureNotUndefined(ensureNotNull(a)); // T & {}
let a2 = ensureNotNull(ensureNotUndefined(a)); // T & {}
let a3 = ensureNotNull(ensureNotNull(a)); // T & {} | T & undefined
let a4 = ensureNotUndefined(ensureNotUndefined(a)); // T & {} | T & null
let a5 = ensureNotNullOrUndefined(ensureNotNullOrUndefined(a)); // T & {}
let a6 = ensureNotNull(possiblyNullOrUndefined(a)); // T & {} | undefined
let a7 = ensureNotUndefined(possiblyNullOrUndefined(a)); // T & {} | null
let a8 = ensureNotNull(possiblyUndefined(a)); // T & {} | undefined
let a9 = ensureNotUndefined(possiblyNull(a)); // T & {} | null
}
// Repro from #48468
function deepEquals<T>(a: T, b: T): boolean {
if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) {
return false;
}
if (Array.isArray(a) || Array.isArray(b)) {
return false;
}
if (Object.keys(a).length !== Object.keys(b).length) { // Error here
return false;
}
return true;
}
@@ -0,0 +1,526 @@
//// [unknownControlFlow.ts]
type T01 = {} & string; // string
type T02 = {} & 'a'; // 'a'
type T03 = {} & object; // object
type T04 = {} & { x: number }; // { x: number }
type T05 = {} & null; // never
type T06 = {} & undefined; // never
type T07 = undefined & void; // undefined
type T10 = string & {}; // Specially preserved
type T11 = number & {}; // Specially preserved
type T12 = bigint & {}; // Specially preserved
type ThisNode = {};
type ThatNode = {};
type ThisOrThatNode = ThisNode | ThatNode;
function f01(u: unknown) {
let x1: {} = u; // Error
let x2: {} | null | undefined = u;
let x3: {} | { x: string } | null | undefined = u;
let x4: ThisOrThatNode | null | undefined = u;
}
function f10(x: unknown) {
if (x) {
x; // {}
}
else {
x; // unknown
}
if (!x) {
x; // unknown
}
else {
x; // {}
}
}
function f11<T>(x: T) {
if (x) {
x; // T & {}
}
else {
x; // T
}
if (!x) {
x; // T
}
else {
x; // T & {}
}
}
function f12<T extends {}>(x: T) {
if (x) {
x; // T
}
else {
x; // T
}
}
function f20(x: unknown) {
if (x !== undefined) {
x; // {} | null
}
else {
x; // undefined
}
if (x !== null) {
x; // {} | undefined
}
else {
x; // null
}
if (x !== undefined && x !== null) {
x; // {}
}
else {
x; // null | undefined
}
if (x != undefined) {
x; // {}
}
else {
x; // null | undefined
}
if (x != null) {
x; // {}
}
else {
x; // null | undefined
}
}
function f21<T>(x: T) {
if (x !== undefined) {
x; // T & ({} | null)
}
else {
x; // T
}
if (x !== null) {
x; // T & ({} | undefined)
}
else {
x; // T
}
if (x !== undefined && x !== null) {
x; // T & {}
}
else {
x; // T
}
if (x != undefined) {
x; // T & {}
}
else {
x; // T
}
if (x != null) {
x; // T & {}
}
else {
x; // T
}
}
function f22<T extends {} | undefined>(x: T) {
if (x !== undefined) {
x; // T & {}
}
else {
x; // T
}
if (x !== null) {
x; // T
}
else {
x; // T
}
if (x !== undefined && x !== null) {
x; // T & {}
}
else {
x; // T
}
if (x != undefined) {
x; // T & {}
}
else {
x; // T
}
if (x != null) {
x; // T & {}
}
else {
x; // T
}
}
function f23<T>(x: T | undefined | null) {
if (x !== undefined) {
x; // T & {} | null
}
if (x !== null) {
x; // T & {} | undefined
}
if (x != undefined) {
x; // T & {}
}
if (x != null) {
x; // T & {}
}
}
function f30(x: {}) {
if (typeof x === "object") {
x; // object
}
}
function f31<T>(x: T) {
if (typeof x === "object") {
x; // T & object | T & null
}
if (x && typeof x === "object") {
x; // T & object
}
if (typeof x === "object" && x) {
x; // T & object
}
}
function f32<T extends {} | undefined>(x: T) {
if (typeof x === "object") {
x; // T & object
}
}
function possiblyNull<T>(x: T) {
return !!true ? x : null; // T | null
}
function possiblyUndefined<T>(x: T) {
return !!true ? x : undefined; // T | undefined
}
function possiblyNullOrUndefined<T>(x: T) {
return possiblyUndefined(possiblyNull(x)); // T | null | undefined
}
function ensureNotNull<T>(x: T) {
if (x === null) throw Error();
return x; // T & ({} | undefined)
}
function ensureNotUndefined<T>(x: T) {
if (x === undefined) throw Error();
return x; // T & ({} | null)
}
function ensureNotNullOrUndefined<T>(x: T) {
return ensureNotUndefined(ensureNotNull(x)); // T & {}
}
function f40(a: string | undefined, b: number | null | undefined) {
let a1 = ensureNotNullOrUndefined(a); // string
let b1 = ensureNotNullOrUndefined(b); // number
}
type QQ<T> = NonNullable<NonNullable<NonNullable<T>>>;
function f41<T>(a: T) {
let a1 = ensureNotUndefined(ensureNotNull(a)); // T & {}
let a2 = ensureNotNull(ensureNotUndefined(a)); // T & {}
let a3 = ensureNotNull(ensureNotNull(a)); // T & {} | T & undefined
let a4 = ensureNotUndefined(ensureNotUndefined(a)); // T & {} | T & null
let a5 = ensureNotNullOrUndefined(ensureNotNullOrUndefined(a)); // T & {}
let a6 = ensureNotNull(possiblyNullOrUndefined(a)); // T & {} | undefined
let a7 = ensureNotUndefined(possiblyNullOrUndefined(a)); // T & {} | null
let a8 = ensureNotNull(possiblyUndefined(a)); // T & {} | undefined
let a9 = ensureNotUndefined(possiblyNull(a)); // T & {} | null
}
// Repro from #48468
function deepEquals<T>(a: T, b: T): boolean {
if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) {
return false;
}
if (Array.isArray(a) || Array.isArray(b)) {
return false;
}
if (Object.keys(a).length !== Object.keys(b).length) { // Error here
return false;
}
return true;
}
//// [unknownControlFlow.js]
"use strict";
function f01(u) {
var x1 = u; // Error
var x2 = u;
var x3 = u;
var x4 = u;
}
function f10(x) {
if (x) {
x; // {}
}
else {
x; // unknown
}
if (!x) {
x; // unknown
}
else {
x; // {}
}
}
function f11(x) {
if (x) {
x; // T & {}
}
else {
x; // T
}
if (!x) {
x; // T
}
else {
x; // T & {}
}
}
function f12(x) {
if (x) {
x; // T
}
else {
x; // T
}
}
function f20(x) {
if (x !== undefined) {
x; // {} | null
}
else {
x; // undefined
}
if (x !== null) {
x; // {} | undefined
}
else {
x; // null
}
if (x !== undefined && x !== null) {
x; // {}
}
else {
x; // null | undefined
}
if (x != undefined) {
x; // {}
}
else {
x; // null | undefined
}
if (x != null) {
x; // {}
}
else {
x; // null | undefined
}
}
function f21(x) {
if (x !== undefined) {
x; // T & ({} | null)
}
else {
x; // T
}
if (x !== null) {
x; // T & ({} | undefined)
}
else {
x; // T
}
if (x !== undefined && x !== null) {
x; // T & {}
}
else {
x; // T
}
if (x != undefined) {
x; // T & {}
}
else {
x; // T
}
if (x != null) {
x; // T & {}
}
else {
x; // T
}
}
function f22(x) {
if (x !== undefined) {
x; // T & {}
}
else {
x; // T
}
if (x !== null) {
x; // T
}
else {
x; // T
}
if (x !== undefined && x !== null) {
x; // T & {}
}
else {
x; // T
}
if (x != undefined) {
x; // T & {}
}
else {
x; // T
}
if (x != null) {
x; // T & {}
}
else {
x; // T
}
}
function f23(x) {
if (x !== undefined) {
x; // T & {} | null
}
if (x !== null) {
x; // T & {} | undefined
}
if (x != undefined) {
x; // T & {}
}
if (x != null) {
x; // T & {}
}
}
function f30(x) {
if (typeof x === "object") {
x; // object
}
}
function f31(x) {
if (typeof x === "object") {
x; // T & object | T & null
}
if (x && typeof x === "object") {
x; // T & object
}
if (typeof x === "object" && x) {
x; // T & object
}
}
function f32(x) {
if (typeof x === "object") {
x; // T & object
}
}
function possiblyNull(x) {
return !!true ? x : null; // T | null
}
function possiblyUndefined(x) {
return !!true ? x : undefined; // T | undefined
}
function possiblyNullOrUndefined(x) {
return possiblyUndefined(possiblyNull(x)); // T | null | undefined
}
function ensureNotNull(x) {
if (x === null)
throw Error();
return x; // T & ({} | undefined)
}
function ensureNotUndefined(x) {
if (x === undefined)
throw Error();
return x; // T & ({} | null)
}
function ensureNotNullOrUndefined(x) {
return ensureNotUndefined(ensureNotNull(x)); // T & {}
}
function f40(a, b) {
var a1 = ensureNotNullOrUndefined(a); // string
var b1 = ensureNotNullOrUndefined(b); // number
}
function f41(a) {
var a1 = ensureNotUndefined(ensureNotNull(a)); // T & {}
var a2 = ensureNotNull(ensureNotUndefined(a)); // T & {}
var a3 = ensureNotNull(ensureNotNull(a)); // T & {} | T & undefined
var a4 = ensureNotUndefined(ensureNotUndefined(a)); // T & {} | T & null
var a5 = ensureNotNullOrUndefined(ensureNotNullOrUndefined(a)); // T & {}
var a6 = ensureNotNull(possiblyNullOrUndefined(a)); // T & {} | undefined
var a7 = ensureNotUndefined(possiblyNullOrUndefined(a)); // T & {} | null
var a8 = ensureNotNull(possiblyUndefined(a)); // T & {} | undefined
var a9 = ensureNotUndefined(possiblyNull(a)); // T & {} | null
}
// Repro from #48468
function deepEquals(a, b) {
if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) {
return false;
}
if (Array.isArray(a) || Array.isArray(b)) {
return false;
}
if (Object.keys(a).length !== Object.keys(b).length) { // Error here
return false;
}
return true;
}
//// [unknownControlFlow.d.ts]
declare type T01 = {} & string;
declare type T02 = {} & 'a';
declare type T03 = {} & object;
declare type T04 = {} & {
x: number;
};
declare type T05 = {} & null;
declare type T06 = {} & undefined;
declare type T07 = undefined & void;
declare type T10 = string & {};
declare type T11 = number & {};
declare type T12 = bigint & {};
declare type ThisNode = {};
declare type ThatNode = {};
declare type ThisOrThatNode = ThisNode | ThatNode;
declare function f01(u: unknown): void;
declare function f10(x: unknown): void;
declare function f11<T>(x: T): void;
declare function f12<T extends {}>(x: T): void;
declare function f20(x: unknown): void;
declare function f21<T>(x: T): void;
declare function f22<T extends {} | undefined>(x: T): void;
declare function f23<T>(x: T | undefined | null): void;
declare function f30(x: {}): void;
declare function f31<T>(x: T): void;
declare function f32<T extends {} | undefined>(x: T): void;
declare function possiblyNull<T>(x: T): T | null;
declare function possiblyUndefined<T>(x: T): T | undefined;
declare function possiblyNullOrUndefined<T>(x: T): T | null | undefined;
declare function ensureNotNull<T>(x: T): T & ({} | undefined);
declare function ensureNotUndefined<T>(x: T): T & ({} | null);
declare function ensureNotNullOrUndefined<T>(x: T): T & {};
declare function f40(a: string | undefined, b: number | null | undefined): void;
declare type QQ<T> = NonNullable<NonNullable<NonNullable<T>>>;
declare function f41<T>(a: T): void;
declare function deepEquals<T>(a: T, b: T): boolean;
@@ -0,0 +1,618 @@
=== tests/cases/conformance/types/unknown/unknownControlFlow.ts ===
type T01 = {} & string; // string
>T01 : Symbol(T01, Decl(unknownControlFlow.ts, 0, 0))
type T02 = {} & 'a'; // 'a'
>T02 : Symbol(T02, Decl(unknownControlFlow.ts, 0, 23))
type T03 = {} & object; // object
>T03 : Symbol(T03, Decl(unknownControlFlow.ts, 1, 20))
type T04 = {} & { x: number }; // { x: number }
>T04 : Symbol(T04, Decl(unknownControlFlow.ts, 2, 23))
>x : Symbol(x, Decl(unknownControlFlow.ts, 3, 17))
type T05 = {} & null; // never
>T05 : Symbol(T05, Decl(unknownControlFlow.ts, 3, 30))
type T06 = {} & undefined; // never
>T06 : Symbol(T06, Decl(unknownControlFlow.ts, 4, 21))
type T07 = undefined & void; // undefined
>T07 : Symbol(T07, Decl(unknownControlFlow.ts, 5, 26))
type T10 = string & {}; // Specially preserved
>T10 : Symbol(T10, Decl(unknownControlFlow.ts, 6, 28))
type T11 = number & {}; // Specially preserved
>T11 : Symbol(T11, Decl(unknownControlFlow.ts, 8, 23))
type T12 = bigint & {}; // Specially preserved
>T12 : Symbol(T12, Decl(unknownControlFlow.ts, 9, 23))
type ThisNode = {};
>ThisNode : Symbol(ThisNode, Decl(unknownControlFlow.ts, 10, 23))
type ThatNode = {};
>ThatNode : Symbol(ThatNode, Decl(unknownControlFlow.ts, 12, 19))
type ThisOrThatNode = ThisNode | ThatNode;
>ThisOrThatNode : Symbol(ThisOrThatNode, Decl(unknownControlFlow.ts, 13, 19))
>ThisNode : Symbol(ThisNode, Decl(unknownControlFlow.ts, 10, 23))
>ThatNode : Symbol(ThatNode, Decl(unknownControlFlow.ts, 12, 19))
function f01(u: unknown) {
>f01 : Symbol(f01, Decl(unknownControlFlow.ts, 14, 42))
>u : Symbol(u, Decl(unknownControlFlow.ts, 16, 13))
let x1: {} = u; // Error
>x1 : Symbol(x1, Decl(unknownControlFlow.ts, 17, 7))
>u : Symbol(u, Decl(unknownControlFlow.ts, 16, 13))
let x2: {} | null | undefined = u;
>x2 : Symbol(x2, Decl(unknownControlFlow.ts, 18, 7))
>u : Symbol(u, Decl(unknownControlFlow.ts, 16, 13))
let x3: {} | { x: string } | null | undefined = u;
>x3 : Symbol(x3, Decl(unknownControlFlow.ts, 19, 7))
>x : Symbol(x, Decl(unknownControlFlow.ts, 19, 18))
>u : Symbol(u, Decl(unknownControlFlow.ts, 16, 13))
let x4: ThisOrThatNode | null | undefined = u;
>x4 : Symbol(x4, Decl(unknownControlFlow.ts, 20, 7))
>ThisOrThatNode : Symbol(ThisOrThatNode, Decl(unknownControlFlow.ts, 13, 19))
>u : Symbol(u, Decl(unknownControlFlow.ts, 16, 13))
}
function f10(x: unknown) {
>f10 : Symbol(f10, Decl(unknownControlFlow.ts, 21, 1))
>x : Symbol(x, Decl(unknownControlFlow.ts, 23, 13))
if (x) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 23, 13))
x; // {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 23, 13))
}
else {
x; // unknown
>x : Symbol(x, Decl(unknownControlFlow.ts, 23, 13))
}
if (!x) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 23, 13))
x; // unknown
>x : Symbol(x, Decl(unknownControlFlow.ts, 23, 13))
}
else {
x; // {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 23, 13))
}
}
function f11<T>(x: T) {
>f11 : Symbol(f11, Decl(unknownControlFlow.ts, 36, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 38, 13))
>x : Symbol(x, Decl(unknownControlFlow.ts, 38, 16))
>T : Symbol(T, Decl(unknownControlFlow.ts, 38, 13))
if (x) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 38, 16))
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 38, 16))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 38, 16))
}
if (!x) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 38, 16))
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 38, 16))
}
else {
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 38, 16))
}
}
function f12<T extends {}>(x: T) {
>f12 : Symbol(f12, Decl(unknownControlFlow.ts, 51, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 53, 13))
>x : Symbol(x, Decl(unknownControlFlow.ts, 53, 27))
>T : Symbol(T, Decl(unknownControlFlow.ts, 53, 13))
if (x) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 53, 27))
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 53, 27))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 53, 27))
}
}
function f20(x: unknown) {
>f20 : Symbol(f20, Decl(unknownControlFlow.ts, 60, 1))
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
if (x !== undefined) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
>undefined : Symbol(undefined)
x; // {} | null
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
}
else {
x; // undefined
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
}
if (x !== null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
x; // {} | undefined
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
}
else {
x; // null
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
}
if (x !== undefined && x !== null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
>undefined : Symbol(undefined)
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
x; // {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
}
else {
x; // null | undefined
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
}
if (x != undefined) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
>undefined : Symbol(undefined)
x; // {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
}
else {
x; // null | undefined
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
}
if (x != null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
x; // {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
}
else {
x; // null | undefined
>x : Symbol(x, Decl(unknownControlFlow.ts, 62, 13))
}
}
function f21<T>(x: T) {
>f21 : Symbol(f21, Decl(unknownControlFlow.ts, 93, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 95, 13))
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
>T : Symbol(T, Decl(unknownControlFlow.ts, 95, 13))
if (x !== undefined) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
>undefined : Symbol(undefined)
x; // T & ({} | null)
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
}
if (x !== null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
x; // T & ({} | undefined)
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
}
if (x !== undefined && x !== null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
>undefined : Symbol(undefined)
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
}
if (x != undefined) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
>undefined : Symbol(undefined)
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
}
if (x != null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 95, 16))
}
}
function f22<T extends {} | undefined>(x: T) {
>f22 : Symbol(f22, Decl(unknownControlFlow.ts, 126, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 128, 13))
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
>T : Symbol(T, Decl(unknownControlFlow.ts, 128, 13))
if (x !== undefined) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
>undefined : Symbol(undefined)
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
}
if (x !== null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
}
if (x !== undefined && x !== null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
>undefined : Symbol(undefined)
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
}
if (x != undefined) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
>undefined : Symbol(undefined)
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
}
if (x != null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
}
else {
x; // T
>x : Symbol(x, Decl(unknownControlFlow.ts, 128, 39))
}
}
function f23<T>(x: T | undefined | null) {
>f23 : Symbol(f23, Decl(unknownControlFlow.ts, 159, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 161, 13))
>x : Symbol(x, Decl(unknownControlFlow.ts, 161, 16))
>T : Symbol(T, Decl(unknownControlFlow.ts, 161, 13))
if (x !== undefined) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 161, 16))
>undefined : Symbol(undefined)
x; // T & {} | null
>x : Symbol(x, Decl(unknownControlFlow.ts, 161, 16))
}
if (x !== null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 161, 16))
x; // T & {} | undefined
>x : Symbol(x, Decl(unknownControlFlow.ts, 161, 16))
}
if (x != undefined) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 161, 16))
>undefined : Symbol(undefined)
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 161, 16))
}
if (x != null) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 161, 16))
x; // T & {}
>x : Symbol(x, Decl(unknownControlFlow.ts, 161, 16))
}
}
function f30(x: {}) {
>f30 : Symbol(f30, Decl(unknownControlFlow.ts, 174, 1))
>x : Symbol(x, Decl(unknownControlFlow.ts, 176, 13))
if (typeof x === "object") {
>x : Symbol(x, Decl(unknownControlFlow.ts, 176, 13))
x; // object
>x : Symbol(x, Decl(unknownControlFlow.ts, 176, 13))
}
}
function f31<T>(x: T) {
>f31 : Symbol(f31, Decl(unknownControlFlow.ts, 180, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 182, 13))
>x : Symbol(x, Decl(unknownControlFlow.ts, 182, 16))
>T : Symbol(T, Decl(unknownControlFlow.ts, 182, 13))
if (typeof x === "object") {
>x : Symbol(x, Decl(unknownControlFlow.ts, 182, 16))
x; // T & object | T & null
>x : Symbol(x, Decl(unknownControlFlow.ts, 182, 16))
}
if (x && typeof x === "object") {
>x : Symbol(x, Decl(unknownControlFlow.ts, 182, 16))
>x : Symbol(x, Decl(unknownControlFlow.ts, 182, 16))
x; // T & object
>x : Symbol(x, Decl(unknownControlFlow.ts, 182, 16))
}
if (typeof x === "object" && x) {
>x : Symbol(x, Decl(unknownControlFlow.ts, 182, 16))
>x : Symbol(x, Decl(unknownControlFlow.ts, 182, 16))
x; // T & object
>x : Symbol(x, Decl(unknownControlFlow.ts, 182, 16))
}
}
function f32<T extends {} | undefined>(x: T) {
>f32 : Symbol(f32, Decl(unknownControlFlow.ts, 192, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 194, 13))
>x : Symbol(x, Decl(unknownControlFlow.ts, 194, 39))
>T : Symbol(T, Decl(unknownControlFlow.ts, 194, 13))
if (typeof x === "object") {
>x : Symbol(x, Decl(unknownControlFlow.ts, 194, 39))
x; // T & object
>x : Symbol(x, Decl(unknownControlFlow.ts, 194, 39))
}
}
function possiblyNull<T>(x: T) {
>possiblyNull : Symbol(possiblyNull, Decl(unknownControlFlow.ts, 198, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 200, 22))
>x : Symbol(x, Decl(unknownControlFlow.ts, 200, 25))
>T : Symbol(T, Decl(unknownControlFlow.ts, 200, 22))
return !!true ? x : null; // T | null
>x : Symbol(x, Decl(unknownControlFlow.ts, 200, 25))
}
function possiblyUndefined<T>(x: T) {
>possiblyUndefined : Symbol(possiblyUndefined, Decl(unknownControlFlow.ts, 202, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 204, 27))
>x : Symbol(x, Decl(unknownControlFlow.ts, 204, 30))
>T : Symbol(T, Decl(unknownControlFlow.ts, 204, 27))
return !!true ? x : undefined; // T | undefined
>x : Symbol(x, Decl(unknownControlFlow.ts, 204, 30))
>undefined : Symbol(undefined)
}
function possiblyNullOrUndefined<T>(x: T) {
>possiblyNullOrUndefined : Symbol(possiblyNullOrUndefined, Decl(unknownControlFlow.ts, 206, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 208, 33))
>x : Symbol(x, Decl(unknownControlFlow.ts, 208, 36))
>T : Symbol(T, Decl(unknownControlFlow.ts, 208, 33))
return possiblyUndefined(possiblyNull(x)); // T | null | undefined
>possiblyUndefined : Symbol(possiblyUndefined, Decl(unknownControlFlow.ts, 202, 1))
>possiblyNull : Symbol(possiblyNull, Decl(unknownControlFlow.ts, 198, 1))
>x : Symbol(x, Decl(unknownControlFlow.ts, 208, 36))
}
function ensureNotNull<T>(x: T) {
>ensureNotNull : Symbol(ensureNotNull, Decl(unknownControlFlow.ts, 210, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 212, 23))
>x : Symbol(x, Decl(unknownControlFlow.ts, 212, 26))
>T : Symbol(T, Decl(unknownControlFlow.ts, 212, 23))
if (x === null) throw Error();
>x : Symbol(x, Decl(unknownControlFlow.ts, 212, 26))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
return x; // T & ({} | undefined)
>x : Symbol(x, Decl(unknownControlFlow.ts, 212, 26))
}
function ensureNotUndefined<T>(x: T) {
>ensureNotUndefined : Symbol(ensureNotUndefined, Decl(unknownControlFlow.ts, 215, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 217, 28))
>x : Symbol(x, Decl(unknownControlFlow.ts, 217, 31))
>T : Symbol(T, Decl(unknownControlFlow.ts, 217, 28))
if (x === undefined) throw Error();
>x : Symbol(x, Decl(unknownControlFlow.ts, 217, 31))
>undefined : Symbol(undefined)
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
return x; // T & ({} | null)
>x : Symbol(x, Decl(unknownControlFlow.ts, 217, 31))
}
function ensureNotNullOrUndefined<T>(x: T) {
>ensureNotNullOrUndefined : Symbol(ensureNotNullOrUndefined, Decl(unknownControlFlow.ts, 220, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 222, 34))
>x : Symbol(x, Decl(unknownControlFlow.ts, 222, 37))
>T : Symbol(T, Decl(unknownControlFlow.ts, 222, 34))
return ensureNotUndefined(ensureNotNull(x)); // T & {}
>ensureNotUndefined : Symbol(ensureNotUndefined, Decl(unknownControlFlow.ts, 215, 1))
>ensureNotNull : Symbol(ensureNotNull, Decl(unknownControlFlow.ts, 210, 1))
>x : Symbol(x, Decl(unknownControlFlow.ts, 222, 37))
}
function f40(a: string | undefined, b: number | null | undefined) {
>f40 : Symbol(f40, Decl(unknownControlFlow.ts, 224, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 226, 13))
>b : Symbol(b, Decl(unknownControlFlow.ts, 226, 35))
let a1 = ensureNotNullOrUndefined(a); // string
>a1 : Symbol(a1, Decl(unknownControlFlow.ts, 227, 7))
>ensureNotNullOrUndefined : Symbol(ensureNotNullOrUndefined, Decl(unknownControlFlow.ts, 220, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 226, 13))
let b1 = ensureNotNullOrUndefined(b); // number
>b1 : Symbol(b1, Decl(unknownControlFlow.ts, 228, 7))
>ensureNotNullOrUndefined : Symbol(ensureNotNullOrUndefined, Decl(unknownControlFlow.ts, 220, 1))
>b : Symbol(b, Decl(unknownControlFlow.ts, 226, 35))
}
type QQ<T> = NonNullable<NonNullable<NonNullable<T>>>;
>QQ : Symbol(QQ, Decl(unknownControlFlow.ts, 229, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 231, 8))
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --))
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --))
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(unknownControlFlow.ts, 231, 8))
function f41<T>(a: T) {
>f41 : Symbol(f41, Decl(unknownControlFlow.ts, 231, 54))
>T : Symbol(T, Decl(unknownControlFlow.ts, 233, 13))
>a : Symbol(a, Decl(unknownControlFlow.ts, 233, 16))
>T : Symbol(T, Decl(unknownControlFlow.ts, 233, 13))
let a1 = ensureNotUndefined(ensureNotNull(a)); // T & {}
>a1 : Symbol(a1, Decl(unknownControlFlow.ts, 234, 7))
>ensureNotUndefined : Symbol(ensureNotUndefined, Decl(unknownControlFlow.ts, 215, 1))
>ensureNotNull : Symbol(ensureNotNull, Decl(unknownControlFlow.ts, 210, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 233, 16))
let a2 = ensureNotNull(ensureNotUndefined(a)); // T & {}
>a2 : Symbol(a2, Decl(unknownControlFlow.ts, 235, 7))
>ensureNotNull : Symbol(ensureNotNull, Decl(unknownControlFlow.ts, 210, 1))
>ensureNotUndefined : Symbol(ensureNotUndefined, Decl(unknownControlFlow.ts, 215, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 233, 16))
let a3 = ensureNotNull(ensureNotNull(a)); // T & {} | T & undefined
>a3 : Symbol(a3, Decl(unknownControlFlow.ts, 236, 7))
>ensureNotNull : Symbol(ensureNotNull, Decl(unknownControlFlow.ts, 210, 1))
>ensureNotNull : Symbol(ensureNotNull, Decl(unknownControlFlow.ts, 210, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 233, 16))
let a4 = ensureNotUndefined(ensureNotUndefined(a)); // T & {} | T & null
>a4 : Symbol(a4, Decl(unknownControlFlow.ts, 237, 7))
>ensureNotUndefined : Symbol(ensureNotUndefined, Decl(unknownControlFlow.ts, 215, 1))
>ensureNotUndefined : Symbol(ensureNotUndefined, Decl(unknownControlFlow.ts, 215, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 233, 16))
let a5 = ensureNotNullOrUndefined(ensureNotNullOrUndefined(a)); // T & {}
>a5 : Symbol(a5, Decl(unknownControlFlow.ts, 238, 7))
>ensureNotNullOrUndefined : Symbol(ensureNotNullOrUndefined, Decl(unknownControlFlow.ts, 220, 1))
>ensureNotNullOrUndefined : Symbol(ensureNotNullOrUndefined, Decl(unknownControlFlow.ts, 220, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 233, 16))
let a6 = ensureNotNull(possiblyNullOrUndefined(a)); // T & {} | undefined
>a6 : Symbol(a6, Decl(unknownControlFlow.ts, 239, 7))
>ensureNotNull : Symbol(ensureNotNull, Decl(unknownControlFlow.ts, 210, 1))
>possiblyNullOrUndefined : Symbol(possiblyNullOrUndefined, Decl(unknownControlFlow.ts, 206, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 233, 16))
let a7 = ensureNotUndefined(possiblyNullOrUndefined(a)); // T & {} | null
>a7 : Symbol(a7, Decl(unknownControlFlow.ts, 240, 7))
>ensureNotUndefined : Symbol(ensureNotUndefined, Decl(unknownControlFlow.ts, 215, 1))
>possiblyNullOrUndefined : Symbol(possiblyNullOrUndefined, Decl(unknownControlFlow.ts, 206, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 233, 16))
let a8 = ensureNotNull(possiblyUndefined(a)); // T & {} | undefined
>a8 : Symbol(a8, Decl(unknownControlFlow.ts, 241, 7))
>ensureNotNull : Symbol(ensureNotNull, Decl(unknownControlFlow.ts, 210, 1))
>possiblyUndefined : Symbol(possiblyUndefined, Decl(unknownControlFlow.ts, 202, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 233, 16))
let a9 = ensureNotUndefined(possiblyNull(a)); // T & {} | null
>a9 : Symbol(a9, Decl(unknownControlFlow.ts, 242, 7))
>ensureNotUndefined : Symbol(ensureNotUndefined, Decl(unknownControlFlow.ts, 215, 1))
>possiblyNull : Symbol(possiblyNull, Decl(unknownControlFlow.ts, 198, 1))
>a : Symbol(a, Decl(unknownControlFlow.ts, 233, 16))
}
// Repro from #48468
function deepEquals<T>(a: T, b: T): boolean {
>deepEquals : Symbol(deepEquals, Decl(unknownControlFlow.ts, 243, 1))
>T : Symbol(T, Decl(unknownControlFlow.ts, 247, 20))
>a : Symbol(a, Decl(unknownControlFlow.ts, 247, 23))
>T : Symbol(T, Decl(unknownControlFlow.ts, 247, 20))
>b : Symbol(b, Decl(unknownControlFlow.ts, 247, 28))
>T : Symbol(T, Decl(unknownControlFlow.ts, 247, 20))
if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) {
>a : Symbol(a, Decl(unknownControlFlow.ts, 247, 23))
>b : Symbol(b, Decl(unknownControlFlow.ts, 247, 28))
>a : Symbol(a, Decl(unknownControlFlow.ts, 247, 23))
>b : Symbol(b, Decl(unknownControlFlow.ts, 247, 28))
return false;
}
if (Array.isArray(a) || Array.isArray(b)) {
>Array.isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --))
>a : Symbol(a, Decl(unknownControlFlow.ts, 247, 23))
>Array.isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --))
>b : Symbol(b, Decl(unknownControlFlow.ts, 247, 28))
return false;
}
if (Object.keys(a).length !== Object.keys(b).length) { // Error here
>Object.keys(a).length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>Object.keys : Symbol(ObjectConstructor.keys, Decl(lib.es5.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>keys : Symbol(ObjectConstructor.keys, Decl(lib.es5.d.ts, --, --))
>a : Symbol(a, Decl(unknownControlFlow.ts, 247, 23))
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>Object.keys(b).length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>Object.keys : Symbol(ObjectConstructor.keys, Decl(lib.es5.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>keys : Symbol(ObjectConstructor.keys, Decl(lib.es5.d.ts, --, --))
>b : Symbol(b, Decl(unknownControlFlow.ts, 247, 28))
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
return false;
}
return true;
}
@@ -0,0 +1,699 @@
=== tests/cases/conformance/types/unknown/unknownControlFlow.ts ===
type T01 = {} & string; // string
>T01 : string
type T02 = {} & 'a'; // 'a'
>T02 : "a"
type T03 = {} & object; // object
>T03 : object
type T04 = {} & { x: number }; // { x: number }
>T04 : { x: number; }
>x : number
type T05 = {} & null; // never
>T05 : never
>null : null
type T06 = {} & undefined; // never
>T06 : never
type T07 = undefined & void; // undefined
>T07 : undefined
type T10 = string & {}; // Specially preserved
>T10 : string & {}
type T11 = number & {}; // Specially preserved
>T11 : number & {}
type T12 = bigint & {}; // Specially preserved
>T12 : bigint & {}
type ThisNode = {};
>ThisNode : {}
type ThatNode = {};
>ThatNode : {}
type ThisOrThatNode = ThisNode | ThatNode;
>ThisOrThatNode : ThisNode | ThatNode
function f01(u: unknown) {
>f01 : (u: unknown) => void
>u : unknown
let x1: {} = u; // Error
>x1 : {}
>u : unknown
let x2: {} | null | undefined = u;
>x2 : {} | null | undefined
>null : null
>u : unknown
let x3: {} | { x: string } | null | undefined = u;
>x3 : {} | { x: string; } | null | undefined
>x : string
>null : null
>u : unknown
let x4: ThisOrThatNode | null | undefined = u;
>x4 : ThisOrThatNode | null | undefined
>null : null
>u : unknown
}
function f10(x: unknown) {
>f10 : (x: unknown) => void
>x : unknown
if (x) {
>x : unknown
x; // {}
>x : {}
}
else {
x; // unknown
>x : unknown
}
if (!x) {
>!x : boolean
>x : unknown
x; // unknown
>x : unknown
}
else {
x; // {}
>x : {}
}
}
function f11<T>(x: T) {
>f11 : <T>(x: T) => void
>x : T
if (x) {
>x : T
x; // T & {}
>x : NonNullable<T>
}
else {
x; // T
>x : T
}
if (!x) {
>!x : boolean
>x : T
x; // T
>x : T
}
else {
x; // T & {}
>x : NonNullable<T>
}
}
function f12<T extends {}>(x: T) {
>f12 : <T extends {}>(x: T) => void
>x : T
if (x) {
>x : T
x; // T
>x : T
}
else {
x; // T
>x : T
}
}
function f20(x: unknown) {
>f20 : (x: unknown) => void
>x : unknown
if (x !== undefined) {
>x !== undefined : boolean
>x : unknown
>undefined : undefined
x; // {} | null
>x : {} | null
}
else {
x; // undefined
>x : undefined
}
if (x !== null) {
>x !== null : boolean
>x : unknown
>null : null
x; // {} | undefined
>x : {} | undefined
}
else {
x; // null
>x : null
}
if (x !== undefined && x !== null) {
>x !== undefined && x !== null : boolean
>x !== undefined : boolean
>x : unknown
>undefined : undefined
>x !== null : boolean
>x : {} | null
>null : null
x; // {}
>x : {}
}
else {
x; // null | undefined
>x : null | undefined
}
if (x != undefined) {
>x != undefined : boolean
>x : unknown
>undefined : undefined
x; // {}
>x : {}
}
else {
x; // null | undefined
>x : null | undefined
}
if (x != null) {
>x != null : boolean
>x : unknown
>null : null
x; // {}
>x : {}
}
else {
x; // null | undefined
>x : null | undefined
}
}
function f21<T>(x: T) {
>f21 : <T>(x: T) => void
>x : T
if (x !== undefined) {
>x !== undefined : boolean
>x : T
>undefined : undefined
x; // T & ({} | null)
>x : T & ({} | null)
}
else {
x; // T
>x : T
}
if (x !== null) {
>x !== null : boolean
>x : T
>null : null
x; // T & ({} | undefined)
>x : T & ({} | undefined)
}
else {
x; // T
>x : T
}
if (x !== undefined && x !== null) {
>x !== undefined && x !== null : boolean
>x !== undefined : boolean
>x : T
>undefined : undefined
>x !== null : boolean
>x : T & ({} | null)
>null : null
x; // T & {}
>x : T & {}
}
else {
x; // T
>x : T
}
if (x != undefined) {
>x != undefined : boolean
>x : T
>undefined : undefined
x; // T & {}
>x : NonNullable<T>
}
else {
x; // T
>x : T
}
if (x != null) {
>x != null : boolean
>x : T
>null : null
x; // T & {}
>x : NonNullable<T>
}
else {
x; // T
>x : T
}
}
function f22<T extends {} | undefined>(x: T) {
>f22 : <T extends {} | undefined>(x: T) => void
>x : T
if (x !== undefined) {
>x !== undefined : boolean
>x : T
>undefined : undefined
x; // T & {}
>x : T & {}
}
else {
x; // T
>x : T
}
if (x !== null) {
>x !== null : boolean
>x : T
>null : null
x; // T
>x : T
}
else {
x; // T
>x : never
}
if (x !== undefined && x !== null) {
>x !== undefined && x !== null : boolean
>x !== undefined : boolean
>x : T
>undefined : undefined
>x !== null : boolean
>x : T & {}
>null : null
x; // T & {}
>x : T & {}
}
else {
x; // T
>x : T
}
if (x != undefined) {
>x != undefined : boolean
>x : T
>undefined : undefined
x; // T & {}
>x : NonNullable<T>
}
else {
x; // T
>x : T
}
if (x != null) {
>x != null : boolean
>x : T
>null : null
x; // T & {}
>x : NonNullable<T>
}
else {
x; // T
>x : T
}
}
function f23<T>(x: T | undefined | null) {
>f23 : <T>(x: T | undefined | null) => void
>x : T | null | undefined
>null : null
if (x !== undefined) {
>x !== undefined : boolean
>x : T | null | undefined
>undefined : undefined
x; // T & {} | null
>x : (T & {}) | null
}
if (x !== null) {
>x !== null : boolean
>x : T | null | undefined
>null : null
x; // T & {} | undefined
>x : (T & {}) | undefined
}
if (x != undefined) {
>x != undefined : boolean
>x : T | null | undefined
>undefined : undefined
x; // T & {}
>x : NonNullable<T>
}
if (x != null) {
>x != null : boolean
>x : T | null | undefined
>null : null
x; // T & {}
>x : NonNullable<T>
}
}
function f30(x: {}) {
>f30 : (x: {}) => void
>x : {}
if (typeof x === "object") {
>typeof x === "object" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : {}
>"object" : "object"
x; // object
>x : object
}
}
function f31<T>(x: T) {
>f31 : <T>(x: T) => void
>x : T
if (typeof x === "object") {
>typeof x === "object" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : T
>"object" : "object"
x; // T & object | T & null
>x : (T & null) | (T & object)
}
if (x && typeof x === "object") {
>x && typeof x === "object" : boolean
>x : T
>typeof x === "object" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : NonNullable<T>
>"object" : "object"
x; // T & object
>x : T & object
}
if (typeof x === "object" && x) {
>typeof x === "object" && x : false | (T & null) | (T & object)
>typeof x === "object" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : T
>"object" : "object"
>x : (T & null) | (T & object)
x; // T & object
>x : T & object
}
}
function f32<T extends {} | undefined>(x: T) {
>f32 : <T extends {} | undefined>(x: T) => void
>x : T
if (typeof x === "object") {
>typeof x === "object" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : T
>"object" : "object"
x; // T & object
>x : T & object
}
}
function possiblyNull<T>(x: T) {
>possiblyNull : <T>(x: T) => T | null
>x : T
return !!true ? x : null; // T | null
>!!true ? x : null : T | null
>!!true : true
>!true : false
>true : true
>x : T
>null : null
}
function possiblyUndefined<T>(x: T) {
>possiblyUndefined : <T>(x: T) => T | undefined
>x : T
return !!true ? x : undefined; // T | undefined
>!!true ? x : undefined : T | undefined
>!!true : true
>!true : false
>true : true
>x : T
>undefined : undefined
}
function possiblyNullOrUndefined<T>(x: T) {
>possiblyNullOrUndefined : <T>(x: T) => T | null | undefined
>x : T
return possiblyUndefined(possiblyNull(x)); // T | null | undefined
>possiblyUndefined(possiblyNull(x)) : T | null | undefined
>possiblyUndefined : <T>(x: T) => T | undefined
>possiblyNull(x) : T | null
>possiblyNull : <T>(x: T) => T | null
>x : T
}
function ensureNotNull<T>(x: T) {
>ensureNotNull : <T>(x: T) => T & ({} | undefined)
>x : T
if (x === null) throw Error();
>x === null : boolean
>x : T
>null : null
>Error() : Error
>Error : ErrorConstructor
return x; // T & ({} | undefined)
>x : T & ({} | undefined)
}
function ensureNotUndefined<T>(x: T) {
>ensureNotUndefined : <T>(x: T) => T & ({} | null)
>x : T
if (x === undefined) throw Error();
>x === undefined : boolean
>x : T
>undefined : undefined
>Error() : Error
>Error : ErrorConstructor
return x; // T & ({} | null)
>x : T & ({} | null)
}
function ensureNotNullOrUndefined<T>(x: T) {
>ensureNotNullOrUndefined : <T>(x: T) => T & {}
>x : T
return ensureNotUndefined(ensureNotNull(x)); // T & {}
>ensureNotUndefined(ensureNotNull(x)) : T & {}
>ensureNotUndefined : <T>(x: T) => T & ({} | null)
>ensureNotNull(x) : T & ({} | undefined)
>ensureNotNull : <T>(x: T) => T & ({} | undefined)
>x : T
}
function f40(a: string | undefined, b: number | null | undefined) {
>f40 : (a: string | undefined, b: number | null | undefined) => void
>a : string | undefined
>b : number | null | undefined
>null : null
let a1 = ensureNotNullOrUndefined(a); // string
>a1 : string
>ensureNotNullOrUndefined(a) : string
>ensureNotNullOrUndefined : <T>(x: T) => T & {}
>a : string | undefined
let b1 = ensureNotNullOrUndefined(b); // number
>b1 : number
>ensureNotNullOrUndefined(b) : number
>ensureNotNullOrUndefined : <T>(x: T) => T & {}
>b : number | null | undefined
}
type QQ<T> = NonNullable<NonNullable<NonNullable<T>>>;
>QQ : QQ<T>
function f41<T>(a: T) {
>f41 : <T>(a: T) => void
>a : T
let a1 = ensureNotUndefined(ensureNotNull(a)); // T & {}
>a1 : T & {}
>ensureNotUndefined(ensureNotNull(a)) : T & {}
>ensureNotUndefined : <T>(x: T) => T & ({} | null)
>ensureNotNull(a) : T & ({} | undefined)
>ensureNotNull : <T>(x: T) => T & ({} | undefined)
>a : T
let a2 = ensureNotNull(ensureNotUndefined(a)); // T & {}
>a2 : T & {}
>ensureNotNull(ensureNotUndefined(a)) : T & {}
>ensureNotNull : <T>(x: T) => T & ({} | undefined)
>ensureNotUndefined(a) : T & ({} | null)
>ensureNotUndefined : <T>(x: T) => T & ({} | null)
>a : T
let a3 = ensureNotNull(ensureNotNull(a)); // T & {} | T & undefined
>a3 : (T & undefined) | (T & {})
>ensureNotNull(ensureNotNull(a)) : (T & undefined) | (T & {})
>ensureNotNull : <T>(x: T) => T & ({} | undefined)
>ensureNotNull(a) : T & ({} | undefined)
>ensureNotNull : <T>(x: T) => T & ({} | undefined)
>a : T
let a4 = ensureNotUndefined(ensureNotUndefined(a)); // T & {} | T & null
>a4 : (T & {}) | (T & null)
>ensureNotUndefined(ensureNotUndefined(a)) : (T & {}) | (T & null)
>ensureNotUndefined : <T>(x: T) => T & ({} | null)
>ensureNotUndefined(a) : T & ({} | null)
>ensureNotUndefined : <T>(x: T) => T & ({} | null)
>a : T
let a5 = ensureNotNullOrUndefined(ensureNotNullOrUndefined(a)); // T & {}
>a5 : T & {}
>ensureNotNullOrUndefined(ensureNotNullOrUndefined(a)) : T & {}
>ensureNotNullOrUndefined : <T>(x: T) => T & {}
>ensureNotNullOrUndefined(a) : T & {}
>ensureNotNullOrUndefined : <T>(x: T) => T & {}
>a : T
let a6 = ensureNotNull(possiblyNullOrUndefined(a)); // T & {} | undefined
>a6 : (T & {}) | undefined
>ensureNotNull(possiblyNullOrUndefined(a)) : (T & {}) | undefined
>ensureNotNull : <T>(x: T) => T & ({} | undefined)
>possiblyNullOrUndefined(a) : T | null | undefined
>possiblyNullOrUndefined : <T>(x: T) => T | null | undefined
>a : T
let a7 = ensureNotUndefined(possiblyNullOrUndefined(a)); // T & {} | null
>a7 : (T & {}) | null
>ensureNotUndefined(possiblyNullOrUndefined(a)) : (T & {}) | null
>ensureNotUndefined : <T>(x: T) => T & ({} | null)
>possiblyNullOrUndefined(a) : T | null | undefined
>possiblyNullOrUndefined : <T>(x: T) => T | null | undefined
>a : T
let a8 = ensureNotNull(possiblyUndefined(a)); // T & {} | undefined
>a8 : (T & {}) | undefined
>ensureNotNull(possiblyUndefined(a)) : (T & {}) | undefined
>ensureNotNull : <T>(x: T) => T & ({} | undefined)
>possiblyUndefined(a) : T | undefined
>possiblyUndefined : <T>(x: T) => T | undefined
>a : T
let a9 = ensureNotUndefined(possiblyNull(a)); // T & {} | null
>a9 : (T & {}) | null
>ensureNotUndefined(possiblyNull(a)) : (T & {}) | null
>ensureNotUndefined : <T>(x: T) => T & ({} | null)
>possiblyNull(a) : T | null
>possiblyNull : <T>(x: T) => T | null
>a : T
}
// Repro from #48468
function deepEquals<T>(a: T, b: T): boolean {
>deepEquals : <T>(a: T, b: T) => boolean
>a : T
>b : T
if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) {
>typeof a !== 'object' || typeof b !== 'object' || !a || !b : boolean
>typeof a !== 'object' || typeof b !== 'object' || !a : boolean
>typeof a !== 'object' || typeof b !== 'object' : boolean
>typeof a !== 'object' : boolean
>typeof a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>a : T
>'object' : "object"
>typeof b !== 'object' : boolean
>typeof b : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>b : T
>'object' : "object"
>!a : boolean
>a : (T & null) | (T & object)
>!b : boolean
>b : (T & null) | (T & object)
return false;
>false : false
}
if (Array.isArray(a) || Array.isArray(b)) {
>Array.isArray(a) || Array.isArray(b) : boolean
>Array.isArray(a) : boolean
>Array.isArray : (arg: any) => arg is any[]
>Array : ArrayConstructor
>isArray : (arg: any) => arg is any[]
>a : T & object
>Array.isArray(b) : boolean
>Array.isArray : (arg: any) => arg is any[]
>Array : ArrayConstructor
>isArray : (arg: any) => arg is any[]
>b : T & object
return false;
>false : false
}
if (Object.keys(a).length !== Object.keys(b).length) { // Error here
>Object.keys(a).length !== Object.keys(b).length : boolean
>Object.keys(a).length : number
>Object.keys(a) : string[]
>Object.keys : (o: object) => string[]
>Object : ObjectConstructor
>keys : (o: object) => string[]
>a : T & object
>length : number
>Object.keys(b).length : number
>Object.keys(b) : string[]
>Object.keys : (o: object) => string[]
>Object : ObjectConstructor
>keys : (o: object) => string[]
>b : T & object
>length : number
return false;
>false : false
}
return true;
>true : true
}
@@ -14,7 +14,6 @@ tests/cases/conformance/types/unknown/unknownType1.ts(110,9): error TS2322: Type
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'.
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(128,5): error TS2322: Type 'number[]' is not assignable to type '{ [x: string]: unknown; }'.
Index signature for type 'string' is missing in type 'number[]'.
@@ -24,6 +23,7 @@ tests/cases/conformance/types/unknown/unknownType1.ts(144,29): error TS2698: Spr
tests/cases/conformance/types/unknown/unknownType1.ts(150,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/types/unknown/unknownType1.ts(156,14): error TS2700: Rest types may only be created from object types.
tests/cases/conformance/types/unknown/unknownType1.ts(162,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/types/unknown/unknownType1.ts(170,9): error TS2322: Type 'T' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(171,9): error TS2322: Type 'U' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(181,5): error TS2322: Type 'T' is not assignable to type '{}'.
@@ -175,8 +175,6 @@ tests/cases/conformance/types/unknown/unknownType1.ts(181,5): error TS2322: Type
~~
!!! 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'.
}
// Type parameter 'T extends unknown' not related to object
@@ -250,6 +248,9 @@ tests/cases/conformance/types/unknown/unknownType1.ts(181,5): error TS2322: Type
function f30<T, U extends unknown>(t: T, u: U) {
let x: {} = t;
~
!!! error TS2322: Type 'T' is not assignable to type '{}'.
!!! related TS2208 tests/cases/conformance/types/unknown/unknownType1.ts:169:14: This type parameter probably needs an `extends object` constraint.
let y: {} = u;
~
!!! error TS2322: Type 'U' is not assignable to type '{}'.
+1 -1
View File
@@ -640,7 +640,7 @@ function f2(x: unknown): string | undefined {
>undefined : undefined
>typeof x !== 'string' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>x : {} | null
>'string' : "string"
throw new Error();
@@ -12,7 +12,7 @@ function isDefined<T>(value: T | undefined | null | void): value is T {
>value : void | T | null | undefined
>undefined : undefined
>value !== null : boolean
>value : T | null
>value : (T & {}) | null
>null : null
}
@@ -0,0 +1,262 @@
// @strict: true
// @declaration: true
type T01 = {} & string; // string
type T02 = {} & 'a'; // 'a'
type T03 = {} & object; // object
type T04 = {} & { x: number }; // { x: number }
type T05 = {} & null; // never
type T06 = {} & undefined; // never
type T07 = undefined & void; // undefined
type T10 = string & {}; // Specially preserved
type T11 = number & {}; // Specially preserved
type T12 = bigint & {}; // Specially preserved
type ThisNode = {};
type ThatNode = {};
type ThisOrThatNode = ThisNode | ThatNode;
function f01(u: unknown) {
let x1: {} = u; // Error
let x2: {} | null | undefined = u;
let x3: {} | { x: string } | null | undefined = u;
let x4: ThisOrThatNode | null | undefined = u;
}
function f10(x: unknown) {
if (x) {
x; // {}
}
else {
x; // unknown
}
if (!x) {
x; // unknown
}
else {
x; // {}
}
}
function f11<T>(x: T) {
if (x) {
x; // T & {}
}
else {
x; // T
}
if (!x) {
x; // T
}
else {
x; // T & {}
}
}
function f12<T extends {}>(x: T) {
if (x) {
x; // T
}
else {
x; // T
}
}
function f20(x: unknown) {
if (x !== undefined) {
x; // {} | null
}
else {
x; // undefined
}
if (x !== null) {
x; // {} | undefined
}
else {
x; // null
}
if (x !== undefined && x !== null) {
x; // {}
}
else {
x; // null | undefined
}
if (x != undefined) {
x; // {}
}
else {
x; // null | undefined
}
if (x != null) {
x; // {}
}
else {
x; // null | undefined
}
}
function f21<T>(x: T) {
if (x !== undefined) {
x; // T & ({} | null)
}
else {
x; // T
}
if (x !== null) {
x; // T & ({} | undefined)
}
else {
x; // T
}
if (x !== undefined && x !== null) {
x; // T & {}
}
else {
x; // T
}
if (x != undefined) {
x; // T & {}
}
else {
x; // T
}
if (x != null) {
x; // T & {}
}
else {
x; // T
}
}
function f22<T extends {} | undefined>(x: T) {
if (x !== undefined) {
x; // T & {}
}
else {
x; // T
}
if (x !== null) {
x; // T
}
else {
x; // T
}
if (x !== undefined && x !== null) {
x; // T & {}
}
else {
x; // T
}
if (x != undefined) {
x; // T & {}
}
else {
x; // T
}
if (x != null) {
x; // T & {}
}
else {
x; // T
}
}
function f23<T>(x: T | undefined | null) {
if (x !== undefined) {
x; // T & {} | null
}
if (x !== null) {
x; // T & {} | undefined
}
if (x != undefined) {
x; // T & {}
}
if (x != null) {
x; // T & {}
}
}
function f30(x: {}) {
if (typeof x === "object") {
x; // object
}
}
function f31<T>(x: T) {
if (typeof x === "object") {
x; // T & object | T & null
}
if (x && typeof x === "object") {
x; // T & object
}
if (typeof x === "object" && x) {
x; // T & object
}
}
function f32<T extends {} | undefined>(x: T) {
if (typeof x === "object") {
x; // T & object
}
}
function possiblyNull<T>(x: T) {
return !!true ? x : null; // T | null
}
function possiblyUndefined<T>(x: T) {
return !!true ? x : undefined; // T | undefined
}
function possiblyNullOrUndefined<T>(x: T) {
return possiblyUndefined(possiblyNull(x)); // T | null | undefined
}
function ensureNotNull<T>(x: T) {
if (x === null) throw Error();
return x; // T & ({} | undefined)
}
function ensureNotUndefined<T>(x: T) {
if (x === undefined) throw Error();
return x; // T & ({} | null)
}
function ensureNotNullOrUndefined<T>(x: T) {
return ensureNotUndefined(ensureNotNull(x)); // T & {}
}
function f40(a: string | undefined, b: number | null | undefined) {
let a1 = ensureNotNullOrUndefined(a); // string
let b1 = ensureNotNullOrUndefined(b); // number
}
type QQ<T> = NonNullable<NonNullable<NonNullable<T>>>;
function f41<T>(a: T) {
let a1 = ensureNotUndefined(ensureNotNull(a)); // T & {}
let a2 = ensureNotNull(ensureNotUndefined(a)); // T & {}
let a3 = ensureNotNull(ensureNotNull(a)); // T & {} | T & undefined
let a4 = ensureNotUndefined(ensureNotUndefined(a)); // T & {} | T & null
let a5 = ensureNotNullOrUndefined(ensureNotNullOrUndefined(a)); // T & {}
let a6 = ensureNotNull(possiblyNullOrUndefined(a)); // T & {} | undefined
let a7 = ensureNotUndefined(possiblyNullOrUndefined(a)); // T & {} | null
let a8 = ensureNotNull(possiblyUndefined(a)); // T & {} | undefined
let a9 = ensureNotUndefined(possiblyNull(a)); // T & {} | null
}
// Repro from #48468
function deepEquals<T>(a: T, b: T): boolean {
if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) {
return false;
}
if (Array.isArray(a) || Array.isArray(b)) {
return false;
}
if (Object.keys(a).length !== Object.keys(b).length) { // Error here
return false;
}
return true;
}
@@ -0,0 +1,20 @@
/// <reference path="fourslash.ts" />
////declare let v1: string & {} | "a" | "b" | "c";
////v1 = "/*1*/";
////declare let v2: number & {} | 0 | 1 | 2;
////v2 = /*2*/;
////declare let v3: string & Record<never, never> | "a" | "b" | "c";
////v3 = "/*3*/";
////type LiteralUnion1<T extends U, U> = T | U & {};
////type LiteralUnion2<T extends U, U> = T | U & Record<never, never>;
////declare let v4: LiteralUnion1<"a" | "b" | "c", string>;
////v4 = "/*4*/";
////declare let v5: LiteralUnion2<"a" | "b" | "c", string>;
////v5 = "/*5*/";
verify.completions({ marker: "1", includes: ["a", "b", "c"] });
verify.completions({ marker: "2", includes: ["0", "1", "2"], isNewIdentifierLocation: true });
verify.completions({ marker: "3", includes: ["a", "b", "c"] });
verify.completions({ marker: "4", excludes: ["a", "b", "c"] });
verify.completions({ marker: "5", includes: ["a", "b", "c"] });