Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues + Support template literal types as discriminants #46137

Merged
merged 3 commits into from
Sep 30, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix issues + Support template literal types in discriminants
  • Loading branch information
ahejlsberg committed Sep 29, 2021
commit 7f347928fbae689f6fdedae471e3ec29d76bb963
44 changes: 30 additions & 14 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12029,7 +12029,7 @@ namespace ts {
else if (type !== firstType) {
checkFlags |= CheckFlags.HasNonUniformType;
}
if (isLiteralType(type)) {
if (isLiteralType(type) || isPatternLiteralType(type)) {
checkFlags |= CheckFlags.HasLiteralType;
}
if (type.flags & TypeFlags.Never) {
Expand Down Expand Up @@ -18935,6 +18935,9 @@ namespace ts {
}
else if (target.flags & TypeFlags.TemplateLiteral) {
if (source.flags & TypeFlags.TemplateLiteral) {
if (relation === comparableRelation) {
return templateLiteralTypesDefinitelyUnrelated(source as TemplateLiteralType, target as TemplateLiteralType) ? Ternary.False : Ternary.True;
}
// Report unreliable variance for type variables referenced in template literal type placeholders.
// For example, `foo-${number}` is related to `foo-${string}` even though number isn't related to string.
instantiateType(source, makeFunctionTypeMapper(reportUnreliableMarkers));
Expand Down Expand Up @@ -18974,11 +18977,10 @@ namespace ts {
return result;
}
}
else if (source.flags & TypeFlags.TemplateLiteral) {
else if (source.flags & TypeFlags.TemplateLiteral && !(target.flags & TypeFlags.Object)) {
if (!(target.flags & TypeFlags.TemplateLiteral)) {
const baseConstraint = getBaseConstraintOfType(source);
const constraint = baseConstraint && baseConstraint !== source ? baseConstraint : stringType;
if (result = isRelatedTo(constraint, target, reportErrors)) {
const constraint = getBaseConstraintOfType(source);
if (constraint && constraint !== source && (result = isRelatedTo(constraint, target, reportErrors))) {
resetErrorInfo(saveErrorInfo);
return result;
}
Expand Down Expand Up @@ -21349,6 +21351,18 @@ namespace ts {
return !!(type.symbol && some(type.symbol.declarations, hasSkipDirectInferenceFlag));
}

function templateLiteralTypesDefinitelyUnrelated(source: TemplateLiteralType, target: TemplateLiteralType) {
// Two template literal types with diffences in their starting or ending text spans are definitely unrelated.
const sourceStart = source.texts[0];
const targetStart = target.texts[0];
const sourceEnd = source.texts[source.texts.length - 1];
const targetEnd = target.texts[target.texts.length - 1];
const startLen = Math.min(sourceStart.length, targetStart.length);
const endLen = Math.min(sourceEnd.length, targetEnd.length);
return sourceStart.slice(0, startLen) !== targetStart.slice(0, startLen) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this flag foo-${boolean} and foo-${number} as possibly related? We still need to make sure the holes relate when this returns True, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it'll flag them as possibly related. Or, rather, not flag them as definitely unrelated. We could potentially check the placeholders that immediately follow identical prefix texts or immediately precede identical suffix texts, but honestly there's very little gained from it. In fact, probably only the boolean vs. number disqualification. I think what is here already is sufficient, and it impacts whether we flag operands as non-overlapping, which isn't super critical to get 100% right.

sourceEnd.slice(sourceEnd.length - endLen) !== targetEnd.slice(targetEnd.length - endLen);
}

function isValidBigIntString(s: string): boolean {
const scanner = createScanner(ScriptTarget.ESNext, /*skipTrivia*/ false);
let success = true;
Expand Down Expand Up @@ -22449,7 +22463,7 @@ namespace ts {
if ((prop as TransientSymbol).isDiscriminantProperty === undefined) {
(prop as TransientSymbol).isDiscriminantProperty =
((prop as TransientSymbol).checkFlags & CheckFlags.Discriminant) === CheckFlags.Discriminant &&
!maybeTypeOfKind(getTypeOfSymbol(prop), TypeFlags.Instantiable & ~TypeFlags.TemplateLiteral);
!isGenericType(getTypeOfSymbol(prop));
}
return !!(prop as TransientSymbol).isDiscriminantProperty;
}
Expand Down Expand Up @@ -23008,15 +23022,17 @@ namespace ts {
return filterType(type, t => (t.flags & kind) !== 0);
}

// Return a new type in which occurrences of the string and number primitive types in
// typeWithPrimitives have been replaced with occurrences of string literals and numeric
// literals in typeWithLiterals, respectively.
// Return a new type in which occurrences of the string, number and bigint primitives and placeholder template
// literal types in typeWithPrimitives have been replaced with occurrences of compatible and more specific types
// from typeWithLiterals. This is essentially a limited form of intersection between the two types. We avoid a
// true intersection because it is more costly and, when applied to union types, generates a large number of
// types we don't actually care about.
function replacePrimitivesWithLiterals(typeWithPrimitives: Type, typeWithLiterals: Type) {
if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.StringLiteral) ||
isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.NumberLiteral) ||
isTypeSubsetOf(bigintType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.BigIntLiteral)) {
if (maybeTypeOfKind(typeWithPrimitives, TypeFlags.String | TypeFlags.TemplateLiteral | TypeFlags.Number | TypeFlags.BigInt) &&
maybeTypeOfKind(typeWithLiterals, TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.NumberLiteral | TypeFlags.BigIntLiteral)) {
return mapType(typeWithPrimitives, t =>
t.flags & TypeFlags.String ? extractTypesOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.StringLiteral) :
t.flags & TypeFlags.String ? extractTypesOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) :
isPatternLiteralType(t) && !maybeTypeOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) ? extractTypesOfKind(typeWithLiterals, TypeFlags.StringLiteral) :
t.flags & TypeFlags.Number ? extractTypesOfKind(typeWithLiterals, TypeFlags.Number | TypeFlags.NumberLiteral) :
t.flags & TypeFlags.BigInt ? extractTypesOfKind(typeWithLiterals, TypeFlags.BigInt | TypeFlags.BigIntLiteral) : t);
}
Expand Down Expand Up @@ -23859,7 +23875,7 @@ namespace ts {
const narrowedPropType = narrowType(propType);
return filterType(type, t => {
const discriminantType = getTypeOfPropertyOrIndexSignature(t, propName);
return !(discriminantType.flags & TypeFlags.Never) && isTypeComparableTo(discriminantType, narrowedPropType);
return !(narrowedPropType.flags & TypeFlags.Never) && isTypeComparableTo(narrowedPropType, discriminantType);
});
}

Expand Down