mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Reduce template literal types with a single placeholder and no extra texts (#55371)
This commit is contained in:
committed by
GitHub
parent
d64646b3fd
commit
a64ea3ae64
@@ -17984,6 +17984,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
if (contains(types, wildcardType)) {
|
||||
return wildcardType;
|
||||
}
|
||||
if (
|
||||
texts.length === 2 && texts[0] === "" && texts[1] === ""
|
||||
// literals (including string enums) are stringified below
|
||||
&& !(types[0].flags & TypeFlags.Literal)
|
||||
// infer T extends StringLike can't be unwrapped eagerly
|
||||
&& !types[0].symbol?.declarations?.some(d => d.parent.kind === SyntaxKind.InferType)
|
||||
&& isTypeAssignableTo(types[0], stringType)
|
||||
) {
|
||||
return types[0];
|
||||
}
|
||||
const newTypes: Type[] = [];
|
||||
const newTexts: string[] = [];
|
||||
let text = texts[0];
|
||||
|
||||
@@ -11,10 +11,10 @@ type T2 = string & `${bigint}`; // `${bigint}
|
||||
>T2 : `${bigint}`
|
||||
|
||||
type T3<T extends string> = string & `${T}`; // `${T}
|
||||
>T3 : `${T}`
|
||||
>T3 : T
|
||||
|
||||
type T4<T extends string> = string & `${Capitalize<`${T}`>}`; // `${Capitalize<T>}`
|
||||
>T4 : `${Capitalize<T>}`
|
||||
>T4 : Capitalize<T>
|
||||
|
||||
function f1(a: boolean[], x: `${number}`) {
|
||||
>f1 : (a: boolean[], x: `${number}`) => void
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -18,7 +18,7 @@ type OriginA1 = `${A}`
|
||||
>OriginA1 : "a"
|
||||
|
||||
type OriginA2 = `${MixA}`
|
||||
>OriginA2 : `${MixA}`
|
||||
>OriginA2 : MixA
|
||||
|
||||
type B = `${typeof a}`
|
||||
>B : "a"
|
||||
@@ -32,23 +32,23 @@ type OriginB1 = `${B}`
|
||||
>OriginB1 : "a"
|
||||
|
||||
type OriginB2 = `${MixB}`
|
||||
>OriginB2 : `${MixB}`
|
||||
>OriginB2 : MixB
|
||||
|
||||
type MixC = { foo: string } & A
|
||||
>MixC : { foo: string; } & "a"
|
||||
>foo : string
|
||||
|
||||
type OriginC = `${MixC}`
|
||||
>OriginC : `${MixC}`
|
||||
>OriginC : MixC
|
||||
|
||||
type MixD<T extends string> =
|
||||
>MixD : `${T & { foo: string; }}`
|
||||
>MixD : T & { foo: string; }
|
||||
|
||||
`${T & { foo: string }}`
|
||||
>foo : string
|
||||
|
||||
type OriginD = `${MixD<A & { foo: string }> & { foo: string }}`;
|
||||
>OriginD : `${`${"a" & { foo: string; } & { foo: string; }}` & { foo: string; }}`
|
||||
>OriginD : "a" & { foo: string; } & { foo: string; } & { foo: string; }
|
||||
>foo : string
|
||||
>foo : string
|
||||
|
||||
|
||||
@@ -31,12 +31,12 @@ options1[`foo/${path}`] = false;
|
||||
|
||||
// Lowercase<`foo/${Path}`> => `foo/${Lowercase<Path>}`
|
||||
declare const lowercasePath: Lowercase<`foo/${Path}`>;
|
||||
>lowercasePath : `foo/${Lowercase<`${Path}`>}`
|
||||
>lowercasePath : `foo/${Lowercase<Path>}`
|
||||
|
||||
options1[lowercasePath] = false;
|
||||
>options1[lowercasePath] = false : false
|
||||
>options1[lowercasePath] : boolean
|
||||
>options1 : { prop: number; } & { [k: string]: boolean; }
|
||||
>lowercasePath : `foo/${Lowercase<`${Path}`>}`
|
||||
>lowercasePath : `foo/${Lowercase<Path>}`
|
||||
>false : false
|
||||
|
||||
|
||||
@@ -93,8 +93,8 @@ function f1<T extends string>(s: string, n: number, b: boolean, t: T) {
|
||||
>b : boolean
|
||||
|
||||
let x7 = foo1(`*${t}*` as const);
|
||||
>x7 : `${T}`
|
||||
>foo1(`*${t}*` as const) : `${T}`
|
||||
>x7 : T
|
||||
>foo1(`*${t}*` as const) : T
|
||||
>foo1 : <V extends string>(arg: `*${V}*`) => V
|
||||
>`*${t}*` as const : `*${T}*`
|
||||
>`*${t}*` : `*${T}*`
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
//// [tests/cases/conformance/types/literal/templateLiteralTypes5.ts] ////
|
||||
|
||||
=== templateLiteralTypes5.ts ===
|
||||
// https://github.com/microsoft/TypeScript/issues/55364
|
||||
interface TypeMap {
|
||||
>TypeMap : Symbol(TypeMap, Decl(templateLiteralTypes5.ts, 0, 0))
|
||||
|
||||
a: "A";
|
||||
>a : Symbol(TypeMap.a, Decl(templateLiteralTypes5.ts, 1, 19))
|
||||
|
||||
b: "B";
|
||||
>b : Symbol(TypeMap.b, Decl(templateLiteralTypes5.ts, 2, 9))
|
||||
}
|
||||
declare const f: <T0 extends "a" | "b">(x: `${T0}`) => TypeMap[T0];
|
||||
>f : Symbol(f, Decl(templateLiteralTypes5.ts, 5, 13))
|
||||
>T0 : Symbol(T0, Decl(templateLiteralTypes5.ts, 5, 18))
|
||||
>x : Symbol(x, Decl(templateLiteralTypes5.ts, 5, 40))
|
||||
>T0 : Symbol(T0, Decl(templateLiteralTypes5.ts, 5, 18))
|
||||
>TypeMap : Symbol(TypeMap, Decl(templateLiteralTypes5.ts, 0, 0))
|
||||
>T0 : Symbol(T0, Decl(templateLiteralTypes5.ts, 5, 18))
|
||||
|
||||
type F1 = <T1 extends "a" | "b">(x: `${T1}`) => TypeMap[T1];
|
||||
>F1 : Symbol(F1, Decl(templateLiteralTypes5.ts, 5, 67))
|
||||
>T1 : Symbol(T1, Decl(templateLiteralTypes5.ts, 6, 11))
|
||||
>x : Symbol(x, Decl(templateLiteralTypes5.ts, 6, 33))
|
||||
>T1 : Symbol(T1, Decl(templateLiteralTypes5.ts, 6, 11))
|
||||
>TypeMap : Symbol(TypeMap, Decl(templateLiteralTypes5.ts, 0, 0))
|
||||
>T1 : Symbol(T1, Decl(templateLiteralTypes5.ts, 6, 11))
|
||||
|
||||
const f1: F1 = f;
|
||||
>f1 : Symbol(f1, Decl(templateLiteralTypes5.ts, 7, 5))
|
||||
>F1 : Symbol(F1, Decl(templateLiteralTypes5.ts, 5, 67))
|
||||
>f : Symbol(f, Decl(templateLiteralTypes5.ts, 5, 13))
|
||||
|
||||
type F2 = <T2 extends 'a' | 'b'>(x: `${T2}`) => TypeMap[`${T2}`]
|
||||
>F2 : Symbol(F2, Decl(templateLiteralTypes5.ts, 7, 17))
|
||||
>T2 : Symbol(T2, Decl(templateLiteralTypes5.ts, 8, 11))
|
||||
>x : Symbol(x, Decl(templateLiteralTypes5.ts, 8, 33))
|
||||
>T2 : Symbol(T2, Decl(templateLiteralTypes5.ts, 8, 11))
|
||||
>TypeMap : Symbol(TypeMap, Decl(templateLiteralTypes5.ts, 0, 0))
|
||||
>T2 : Symbol(T2, Decl(templateLiteralTypes5.ts, 8, 11))
|
||||
|
||||
const f2: F2 = f
|
||||
>f2 : Symbol(f2, Decl(templateLiteralTypes5.ts, 9, 5))
|
||||
>F2 : Symbol(F2, Decl(templateLiteralTypes5.ts, 7, 17))
|
||||
>f : Symbol(f, Decl(templateLiteralTypes5.ts, 5, 13))
|
||||
|
||||
function f3<T3 extends "a" | "b">(x: T3) {
|
||||
>f3 : Symbol(f3, Decl(templateLiteralTypes5.ts, 9, 16))
|
||||
>T3 : Symbol(T3, Decl(templateLiteralTypes5.ts, 11, 12))
|
||||
>x : Symbol(x, Decl(templateLiteralTypes5.ts, 11, 34))
|
||||
>T3 : Symbol(T3, Decl(templateLiteralTypes5.ts, 11, 12))
|
||||
|
||||
const test1: `${T3}` = x
|
||||
>test1 : Symbol(test1, Decl(templateLiteralTypes5.ts, 12, 9))
|
||||
>T3 : Symbol(T3, Decl(templateLiteralTypes5.ts, 11, 12))
|
||||
>x : Symbol(x, Decl(templateLiteralTypes5.ts, 11, 34))
|
||||
|
||||
const test2: T3 = "" as `${T3}`;
|
||||
>test2 : Symbol(test2, Decl(templateLiteralTypes5.ts, 13, 9))
|
||||
>T3 : Symbol(T3, Decl(templateLiteralTypes5.ts, 11, 12))
|
||||
>T3 : Symbol(T3, Decl(templateLiteralTypes5.ts, 11, 12))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
//// [tests/cases/conformance/types/literal/templateLiteralTypes5.ts] ////
|
||||
|
||||
=== templateLiteralTypes5.ts ===
|
||||
// https://github.com/microsoft/TypeScript/issues/55364
|
||||
interface TypeMap {
|
||||
a: "A";
|
||||
>a : "A"
|
||||
|
||||
b: "B";
|
||||
>b : "B"
|
||||
}
|
||||
declare const f: <T0 extends "a" | "b">(x: `${T0}`) => TypeMap[T0];
|
||||
>f : <T0 extends "a" | "b">(x: `${T0}`) => TypeMap[T0]
|
||||
>x : T0
|
||||
|
||||
type F1 = <T1 extends "a" | "b">(x: `${T1}`) => TypeMap[T1];
|
||||
>F1 : <T1 extends "a" | "b">(x: `${T1}`) => TypeMap[T1]
|
||||
>x : T1
|
||||
|
||||
const f1: F1 = f;
|
||||
>f1 : F1
|
||||
>f : <T0 extends "a" | "b">(x: T0) => TypeMap[T0]
|
||||
|
||||
type F2 = <T2 extends 'a' | 'b'>(x: `${T2}`) => TypeMap[`${T2}`]
|
||||
>F2 : <T2 extends "a" | "b">(x: `${T2}`) => TypeMap[`${T2}`]
|
||||
>x : T2
|
||||
|
||||
const f2: F2 = f
|
||||
>f2 : F2
|
||||
>f : <T0 extends "a" | "b">(x: T0) => TypeMap[T0]
|
||||
|
||||
function f3<T3 extends "a" | "b">(x: T3) {
|
||||
>f3 : <T3 extends "a" | "b">(x: T3) => void
|
||||
>x : T3
|
||||
|
||||
const test1: `${T3}` = x
|
||||
>test1 : T3
|
||||
>x : T3
|
||||
|
||||
const test2: T3 = "" as `${T3}`;
|
||||
>test2 : T3
|
||||
>"" as `${T3}` : T3
|
||||
>"" : ""
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
//// [tests/cases/conformance/types/literal/templateLiteralTypes6.ts] ////
|
||||
|
||||
=== templateLiteralTypes6.ts ===
|
||||
// https://github.com/microsoft/TypeScript/issues/56659
|
||||
|
||||
type Registry = {
|
||||
>Registry : Symbol(Registry, Decl(templateLiteralTypes6.ts, 0, 0))
|
||||
|
||||
a: { a1: {} };
|
||||
>a : Symbol(a, Decl(templateLiteralTypes6.ts, 2, 17))
|
||||
>a1 : Symbol(a1, Decl(templateLiteralTypes6.ts, 3, 6))
|
||||
|
||||
b: { b1: {} };
|
||||
>b : Symbol(b, Decl(templateLiteralTypes6.ts, 3, 16))
|
||||
>b1 : Symbol(b1, Decl(templateLiteralTypes6.ts, 4, 6))
|
||||
|
||||
};
|
||||
|
||||
type Keyof<T> = keyof T & string;
|
||||
>Keyof : Symbol(Keyof, Decl(templateLiteralTypes6.ts, 5, 2))
|
||||
>T : Symbol(T, Decl(templateLiteralTypes6.ts, 7, 11))
|
||||
>T : Symbol(T, Decl(templateLiteralTypes6.ts, 7, 11))
|
||||
|
||||
declare function f1<
|
||||
>f1 : Symbol(f1, Decl(templateLiteralTypes6.ts, 7, 33))
|
||||
|
||||
Scope extends Keyof<Registry>,
|
||||
>Scope : Symbol(Scope, Decl(templateLiteralTypes6.ts, 9, 20))
|
||||
>Keyof : Symbol(Keyof, Decl(templateLiteralTypes6.ts, 5, 2))
|
||||
>Registry : Symbol(Registry, Decl(templateLiteralTypes6.ts, 0, 0))
|
||||
|
||||
Event extends Keyof<Registry[Scope]>,
|
||||
>Event : Symbol(Event, Decl(templateLiteralTypes6.ts, 10, 32))
|
||||
>Keyof : Symbol(Keyof, Decl(templateLiteralTypes6.ts, 5, 2))
|
||||
>Registry : Symbol(Registry, Decl(templateLiteralTypes6.ts, 0, 0))
|
||||
>Scope : Symbol(Scope, Decl(templateLiteralTypes6.ts, 9, 20))
|
||||
|
||||
>(eventPath: `${Scope}:${Event}`): void;
|
||||
>eventPath : Symbol(eventPath, Decl(templateLiteralTypes6.ts, 12, 2))
|
||||
>Scope : Symbol(Scope, Decl(templateLiteralTypes6.ts, 9, 20))
|
||||
>Event : Symbol(Event, Decl(templateLiteralTypes6.ts, 10, 32))
|
||||
|
||||
function f2<
|
||||
>f2 : Symbol(f2, Decl(templateLiteralTypes6.ts, 12, 40))
|
||||
|
||||
Scope extends Keyof<Registry>,
|
||||
>Scope : Symbol(Scope, Decl(templateLiteralTypes6.ts, 14, 12))
|
||||
>Keyof : Symbol(Keyof, Decl(templateLiteralTypes6.ts, 5, 2))
|
||||
>Registry : Symbol(Registry, Decl(templateLiteralTypes6.ts, 0, 0))
|
||||
|
||||
Event extends Keyof<Registry[Scope]>,
|
||||
>Event : Symbol(Event, Decl(templateLiteralTypes6.ts, 15, 32))
|
||||
>Keyof : Symbol(Keyof, Decl(templateLiteralTypes6.ts, 5, 2))
|
||||
>Registry : Symbol(Registry, Decl(templateLiteralTypes6.ts, 0, 0))
|
||||
>Scope : Symbol(Scope, Decl(templateLiteralTypes6.ts, 14, 12))
|
||||
|
||||
>(scope: Scope, event: Event) {
|
||||
>scope : Symbol(scope, Decl(templateLiteralTypes6.ts, 17, 2))
|
||||
>Scope : Symbol(Scope, Decl(templateLiteralTypes6.ts, 14, 12))
|
||||
>event : Symbol(event, Decl(templateLiteralTypes6.ts, 17, 15))
|
||||
>Event : Symbol(Event, Decl(templateLiteralTypes6.ts, 15, 32))
|
||||
|
||||
f1(`${scope}:${event}`);
|
||||
>f1 : Symbol(f1, Decl(templateLiteralTypes6.ts, 7, 33))
|
||||
>scope : Symbol(scope, Decl(templateLiteralTypes6.ts, 17, 2))
|
||||
>event : Symbol(event, Decl(templateLiteralTypes6.ts, 17, 15))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
//// [tests/cases/conformance/types/literal/templateLiteralTypes6.ts] ////
|
||||
|
||||
=== templateLiteralTypes6.ts ===
|
||||
// https://github.com/microsoft/TypeScript/issues/56659
|
||||
|
||||
type Registry = {
|
||||
>Registry : { a: { a1: {};}; b: { b1: {};}; }
|
||||
|
||||
a: { a1: {} };
|
||||
>a : { a1: {}; }
|
||||
>a1 : {}
|
||||
|
||||
b: { b1: {} };
|
||||
>b : { b1: {}; }
|
||||
>b1 : {}
|
||||
|
||||
};
|
||||
|
||||
type Keyof<T> = keyof T & string;
|
||||
>Keyof : Keyof<T>
|
||||
|
||||
declare function f1<
|
||||
>f1 : <Scope extends Keyof<Registry>, Event extends Keyof<Registry[Scope]>>(eventPath: `${Scope}:${Event}`) => void
|
||||
|
||||
Scope extends Keyof<Registry>,
|
||||
Event extends Keyof<Registry[Scope]>,
|
||||
>(eventPath: `${Scope}:${Event}`): void;
|
||||
>eventPath : `${Scope}:${Event}`
|
||||
|
||||
function f2<
|
||||
>f2 : <Scope extends Keyof<Registry>, Event extends Keyof<Registry[Scope]>>(scope: Scope, event: Event) => void
|
||||
|
||||
Scope extends Keyof<Registry>,
|
||||
Event extends Keyof<Registry[Scope]>,
|
||||
>(scope: Scope, event: Event) {
|
||||
>scope : Scope
|
||||
>event : Event
|
||||
|
||||
f1(`${scope}:${event}`);
|
||||
>f1(`${scope}:${event}`) : void
|
||||
>f1 : <Scope_1 extends Keyof<Registry>, Event_1 extends Keyof<Registry[Scope_1]>>(eventPath: `${Scope_1}:${Event_1}`) => void
|
||||
>`${scope}:${event}` : `${Scope}:${Event}`
|
||||
>scope : Scope
|
||||
>event : Event
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// @strict: true
|
||||
// @target: esnext
|
||||
// @noEmit: true
|
||||
|
||||
// https://github.com/microsoft/TypeScript/issues/55364
|
||||
interface TypeMap {
|
||||
a: "A";
|
||||
b: "B";
|
||||
}
|
||||
declare const f: <T0 extends "a" | "b">(x: `${T0}`) => TypeMap[T0];
|
||||
type F1 = <T1 extends "a" | "b">(x: `${T1}`) => TypeMap[T1];
|
||||
const f1: F1 = f;
|
||||
type F2 = <T2 extends 'a' | 'b'>(x: `${T2}`) => TypeMap[`${T2}`]
|
||||
const f2: F2 = f
|
||||
|
||||
function f3<T3 extends "a" | "b">(x: T3) {
|
||||
const test1: `${T3}` = x
|
||||
const test2: T3 = "" as `${T3}`;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// @strict: true
|
||||
// @target: esnext
|
||||
// @noEmit: true
|
||||
|
||||
// https://github.com/microsoft/TypeScript/issues/56659
|
||||
|
||||
type Registry = {
|
||||
a: { a1: {} };
|
||||
b: { b1: {} };
|
||||
};
|
||||
|
||||
type Keyof<T> = keyof T & string;
|
||||
|
||||
declare function f1<
|
||||
Scope extends Keyof<Registry>,
|
||||
Event extends Keyof<Registry[Scope]>,
|
||||
>(eventPath: `${Scope}:${Event}`): void;
|
||||
|
||||
function f2<
|
||||
Scope extends Keyof<Registry>,
|
||||
Event extends Keyof<Registry[Scope]>,
|
||||
>(scope: Scope, event: Event) {
|
||||
f1(`${scope}:${event}`);
|
||||
}
|
||||
Reference in New Issue
Block a user