Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
17 changes: 10 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17906,16 +17906,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
accessNode;
}

function isPatternLiteralPlaceholderType(type: Type): boolean {
function isPatternLiteralPlaceholderType(type: Type, ignoreGenericIntersections = false): boolean {
if (type.flags & TypeFlags.Intersection) {
return some((type as IntersectionType).types, t => !!(t.flags & (TypeFlags.Literal | TypeFlags.Null | TypeFlags.Undefined)) || isPatternLiteralPlaceholderType(t));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this entire fix can be simplified to simply adding a check that the type isn't generic:

return !isGenericType(type) && some(...);

Generic types should never be classified as placeholders since upon instantiation they may become something completely different.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's cool! Thanks for the tip. I was worried that addSpans would fail if I did something like this. It seems that (somewhat confusingly) isGenericIndexType already returns true for an intersection like this so addSpans is covered.

if (ignoreGenericIntersections && isGenericType(type)) {
return false;
}
return some((type as IntersectionType).types, t => !!(t.flags & (TypeFlags.Literal | TypeFlags.Nullable)) || isPatternLiteralPlaceholderType(t, ignoreGenericIntersections));
}
return !!(type.flags & (TypeFlags.Any | TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt)) || isPatternLiteralType(type);
return !!(type.flags & (TypeFlags.Any | TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt)) || isPatternLiteralType(type, ignoreGenericIntersections);
}

function isPatternLiteralType(type: Type) {
return !!(type.flags & TypeFlags.TemplateLiteral) && every((type as TemplateLiteralType).types, isPatternLiteralPlaceholderType) ||
!!(type.flags & TypeFlags.StringMapping) && isPatternLiteralPlaceholderType((type as StringMappingType).type);
function isPatternLiteralType(type: Type, ignoreGenericIntersections = false) {
return !!(type.flags & TypeFlags.TemplateLiteral) && every((type as TemplateLiteralType).types, t => isPatternLiteralPlaceholderType(t, ignoreGenericIntersections)) ||
!!(type.flags & TypeFlags.StringMapping) && isPatternLiteralPlaceholderType((type as StringMappingType).type, ignoreGenericIntersections);
}

function isGenericType(type: Type): boolean {
Expand Down Expand Up @@ -17946,7 +17949,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return (type as SubstitutionType).objectFlags & ObjectFlags.IsGenericType;
}
return (type.flags & TypeFlags.InstantiableNonPrimitive || isGenericMappedType(type) || isGenericTupleType(type) ? ObjectFlags.IsGenericObjectType : 0) |
(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0);
(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.StringMapping) && !isPatternLiteralType(type, /*ignoreGenericIntersections*/ true) ? ObjectFlags.IsGenericIndexType : 0);
}

function getSimplifiedType(type: Type, writing: boolean): Type {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//// [tests/cases/conformance/types/literal/stringMappingDeferralInConditionalTypes.ts] ////

=== stringMappingDeferralInConditionalTypes.ts ===
// https://github.com/microsoft/TypeScript/issues/55847

type A<S> = Lowercase<S & string> extends "foo" ? 1 : 0;
>A : Symbol(A, Decl(stringMappingDeferralInConditionalTypes.ts, 0, 0))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 2, 7))
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 2, 7))

let x1: A<"foo"> = 1; // ok
>x1 : Symbol(x1, Decl(stringMappingDeferralInConditionalTypes.ts, 3, 3))
>A : Symbol(A, Decl(stringMappingDeferralInConditionalTypes.ts, 0, 0))

type B<S> = Lowercase<S & string> extends `f${string}` ? 1 : 0;
>B : Symbol(B, Decl(stringMappingDeferralInConditionalTypes.ts, 3, 21))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 5, 7))
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 5, 7))

let x2: B<"foo"> = 1; // ok
>x2 : Symbol(x2, Decl(stringMappingDeferralInConditionalTypes.ts, 6, 3))
>B : Symbol(B, Decl(stringMappingDeferralInConditionalTypes.ts, 3, 21))

type C<S> = Capitalize<Lowercase<S & string>> extends "Foo" ? 1 : 0;
>C : Symbol(C, Decl(stringMappingDeferralInConditionalTypes.ts, 6, 21))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 8, 7))
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --))
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 8, 7))

let x3: C<"foo"> = 1; // ok
>x3 : Symbol(x3, Decl(stringMappingDeferralInConditionalTypes.ts, 9, 3))
>C : Symbol(C, Decl(stringMappingDeferralInConditionalTypes.ts, 6, 21))

type D<S extends string> = Capitalize<Lowercase<S>> extends "Foo" ? 1 : 0;
>D : Symbol(D, Decl(stringMappingDeferralInConditionalTypes.ts, 9, 21))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 11, 7))
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --))
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 11, 7))

let x4: D<"foo"> = 1; // ok
>x4 : Symbol(x4, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 3))
>D : Symbol(D, Decl(stringMappingDeferralInConditionalTypes.ts, 9, 21))

type E<S> = Lowercase<`f${S & string}` & `${S & string}f`>;
>E : Symbol(E, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 21))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 7))
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 7))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 7))

type F = E<""> extends "f" ? 1 : 0; // 1
>F : Symbol(F, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 59))
>E : Symbol(E, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 21))

type G<S> = E<S> extends "f" ? 1 : 0;
>G : Symbol(G, Decl(stringMappingDeferralInConditionalTypes.ts, 15, 35))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 16, 7))
>E : Symbol(E, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 21))
>S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 16, 7))

let x5: G<""> = 1; // ok
>x5 : Symbol(x5, Decl(stringMappingDeferralInConditionalTypes.ts, 17, 3))
>G : Symbol(G, Decl(stringMappingDeferralInConditionalTypes.ts, 15, 35))

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//// [tests/cases/conformance/types/literal/stringMappingDeferralInConditionalTypes.ts] ////

=== stringMappingDeferralInConditionalTypes.ts ===
// https://github.com/microsoft/TypeScript/issues/55847

type A<S> = Lowercase<S & string> extends "foo" ? 1 : 0;
>A : A<S>

let x1: A<"foo"> = 1; // ok
>x1 : 1
>1 : 1

type B<S> = Lowercase<S & string> extends `f${string}` ? 1 : 0;
>B : B<S>

let x2: B<"foo"> = 1; // ok
>x2 : 1
>1 : 1

type C<S> = Capitalize<Lowercase<S & string>> extends "Foo" ? 1 : 0;
>C : C<S>

let x3: C<"foo"> = 1; // ok
>x3 : 1
>1 : 1

type D<S extends string> = Capitalize<Lowercase<S>> extends "Foo" ? 1 : 0;
>D : D<S>

let x4: D<"foo"> = 1; // ok
>x4 : 1
>1 : 1

type E<S> = Lowercase<`f${S & string}` & `${S & string}f`>;
>E : Lowercase<`f${S & string}` & `${S & string}f`>

type F = E<""> extends "f" ? 1 : 0; // 1
>F : 1

type G<S> = E<S> extends "f" ? 1 : 0;
>G : G<S>

let x5: G<""> = 1; // ok
>x5 : 1
>1 : 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/55847

type A<S> = Lowercase<S & string> extends "foo" ? 1 : 0;
let x1: A<"foo"> = 1; // ok

type B<S> = Lowercase<S & string> extends `f${string}` ? 1 : 0;
let x2: B<"foo"> = 1; // ok

type C<S> = Capitalize<Lowercase<S & string>> extends "Foo" ? 1 : 0;
let x3: C<"foo"> = 1; // ok

type D<S extends string> = Capitalize<Lowercase<S>> extends "Foo" ? 1 : 0;
let x4: D<"foo"> = 1; // ok

type E<S> = Lowercase<`f${S & string}` & `${S & string}f`>;
type F = E<""> extends "f" ? 1 : 0; // 1
type G<S> = E<S> extends "f" ? 1 : 0;
let x5: G<""> = 1; // ok