Skip to content

Reinstate constraint check for template literal types #43661

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

Merged
merged 2 commits into from
Apr 13, 2021
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
10 changes: 10 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18402,6 +18402,16 @@ namespace ts {
return result;
}
}
else if (source.flags & TypeFlags.TemplateLiteral) {
if (!(target.flags & TypeFlags.TemplateLiteral)) {
const baseConstraint = getBaseConstraintOfType(source);
const constraint = baseConstraint && baseConstraint !== source ? baseConstraint : stringType;
if (result = isRelatedTo(constraint, target, reportErrors)) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
}
else if (source.flags & TypeFlags.StringMapping) {
if (target.flags & TypeFlags.StringMapping && (<StringMappingType>source).symbol === (<StringMappingType>target).symbol) {
if (result = isRelatedTo((<StringMappingType>source).type, (<StringMappingType>target).type, reportErrors)) {
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/templateLiteralTypes3.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,16 @@ tests/cases/conformance/types/literal/templateLiteralTypes3.ts(74,5): error TS23

const value2 = "abc";
const templated2: Templated = `${value2} abc` as const;

// Repro from #43620

type Prefixes = "foo" | "bar";

type AllPrefixData = "foo:baz" | "bar:baz";

type PrefixData<P extends Prefixes> = `${P}:baz`;

interface ITest<P extends Prefixes, E extends AllPrefixData = PrefixData<P>> {
blah: string;
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/templateLiteralTypes3.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ const templated1: Templated = `${value1} abc` as const;

const value2 = "abc";
const templated2: Templated = `${value2} abc` as const;

// Repro from #43620

type Prefixes = "foo" | "bar";

type AllPrefixData = "foo:baz" | "bar:baz";

type PrefixData<P extends Prefixes> = `${P}:baz`;

interface ITest<P extends Prefixes, E extends AllPrefixData = PrefixData<P>> {
blah: string;
}


//// [templateLiteralTypes3.js]
Expand Down Expand Up @@ -198,3 +210,9 @@ declare const value1: string;
declare const templated1: Templated;
declare const value2 = "abc";
declare const templated2: Templated;
declare type Prefixes = "foo" | "bar";
declare type AllPrefixData = "foo:baz" | "bar:baz";
declare type PrefixData<P extends Prefixes> = `${P}:baz`;
interface ITest<P extends Prefixes, E extends AllPrefixData = PrefixData<P>> {
blah: string;
}
27 changes: 27 additions & 0 deletions tests/baselines/reference/templateLiteralTypes3.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,30 @@ const templated2: Templated = `${value2} abc` as const;
>Templated : Symbol(Templated, Decl(templateLiteralTypes3.ts, 93, 29))
>value2 : Symbol(value2, Decl(templateLiteralTypes3.ts, 103, 5))

// Repro from #43620

type Prefixes = "foo" | "bar";
>Prefixes : Symbol(Prefixes, Decl(templateLiteralTypes3.ts, 104, 55))

type AllPrefixData = "foo:baz" | "bar:baz";
>AllPrefixData : Symbol(AllPrefixData, Decl(templateLiteralTypes3.ts, 108, 30))

type PrefixData<P extends Prefixes> = `${P}:baz`;
>PrefixData : Symbol(PrefixData, Decl(templateLiteralTypes3.ts, 110, 43))
>P : Symbol(P, Decl(templateLiteralTypes3.ts, 112, 16))
>Prefixes : Symbol(Prefixes, Decl(templateLiteralTypes3.ts, 104, 55))
>P : Symbol(P, Decl(templateLiteralTypes3.ts, 112, 16))

interface ITest<P extends Prefixes, E extends AllPrefixData = PrefixData<P>> {
>ITest : Symbol(ITest, Decl(templateLiteralTypes3.ts, 112, 49))
>P : Symbol(P, Decl(templateLiteralTypes3.ts, 114, 16))
>Prefixes : Symbol(Prefixes, Decl(templateLiteralTypes3.ts, 104, 55))
>E : Symbol(E, Decl(templateLiteralTypes3.ts, 114, 35))
>AllPrefixData : Symbol(AllPrefixData, Decl(templateLiteralTypes3.ts, 108, 30))
>PrefixData : Symbol(PrefixData, Decl(templateLiteralTypes3.ts, 110, 43))
>P : Symbol(P, Decl(templateLiteralTypes3.ts, 114, 16))

blah: string;
>blah : Symbol(ITest.blah, Decl(templateLiteralTypes3.ts, 114, 78))
}

16 changes: 16 additions & 0 deletions tests/baselines/reference/templateLiteralTypes3.types
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,19 @@ const templated2: Templated = `${value2} abc` as const;
>`${value2} abc` : "abc abc"
>value2 : "abc"

// Repro from #43620

type Prefixes = "foo" | "bar";
>Prefixes : Prefixes

type AllPrefixData = "foo:baz" | "bar:baz";
>AllPrefixData : AllPrefixData

type PrefixData<P extends Prefixes> = `${P}:baz`;
>PrefixData : `${P}:baz`

interface ITest<P extends Prefixes, E extends AllPrefixData = PrefixData<P>> {
blah: string;
>blah : string
}

12 changes: 12 additions & 0 deletions tests/cases/conformance/types/literal/templateLiteralTypes3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,15 @@ const templated1: Templated = `${value1} abc` as const;

const value2 = "abc";
const templated2: Templated = `${value2} abc` as const;

// Repro from #43620

type Prefixes = "foo" | "bar";

type AllPrefixData = "foo:baz" | "bar:baz";

type PrefixData<P extends Prefixes> = `${P}:baz`;

interface ITest<P extends Prefixes, E extends AllPrefixData = PrefixData<P>> {
blah: string;
}