Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17908,7 +17908,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function isPatternLiteralPlaceholderType(type: Type): 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
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
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.

return !isGenericType(type) && some((type as IntersectionType).types, t => !!(t.flags & (TypeFlags.Literal | TypeFlags.Nullable)) || isPatternLiteralPlaceholderType(t));
}
return !!(type.flags & (TypeFlags.Any | TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt)) || isPatternLiteralType(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