Skip to content

Match suffix _after_ prefix when inferring literals out of templates #40841

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 1 commit into from
Sep 29, 2020
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 @@ -19801,7 +19801,7 @@ namespace ts {
const lastIndex = texts.length - 1;
const startText = texts[0];
const endText = texts[lastIndex];
if (!(value.startsWith(startText) && value.endsWith(endText))) return undefined;
if (!(value.startsWith(startText) && value.slice(startText.length).endsWith(endText))) return undefined;
const matches = [];
const str = value.slice(startText.length, value.length - endText.length);
let pos = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests/cases/conformance/types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts(1,7): error TS2322: Type '":"' is not assignable to type '`:${string}:`'.
tests/cases/conformance/types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts(3,7): error TS2322: Type '"::"' is not assignable to type '`:${string}:${string}:`'.


==== tests/cases/conformance/types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts (2 errors) ====
const s1: `:${string}:` = ":"; // should error
~~
!!! error TS2322: Type '":"' is not assignable to type '`:${string}:`'.
const s2: `:${string}:` = "::"; // ok
const s3: `:${string}:${string}:` = "::"; // should error
~~
!!! error TS2322: Type '"::"' is not assignable to type '`:${string}:${string}:`'.
const s4: `:${string}:${string}:` = ":::"; // ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [templateLiteralTypesPatternsPrefixSuffixAssignability.ts]
const s1: `:${string}:` = ":"; // should error
const s2: `:${string}:` = "::"; // ok
const s3: `:${string}:${string}:` = "::"; // should error
const s4: `:${string}:${string}:` = ":::"; // ok

//// [templateLiteralTypesPatternsPrefixSuffixAssignability.js]
var s1 = ":"; // should error
var s2 = "::"; // ok
var s3 = "::"; // should error
var s4 = ":::"; // ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/conformance/types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts ===
const s1: `:${string}:` = ":"; // should error
>s1 : Symbol(s1, Decl(templateLiteralTypesPatternsPrefixSuffixAssignability.ts, 0, 5))

const s2: `:${string}:` = "::"; // ok
>s2 : Symbol(s2, Decl(templateLiteralTypesPatternsPrefixSuffixAssignability.ts, 1, 5))

const s3: `:${string}:${string}:` = "::"; // should error
>s3 : Symbol(s3, Decl(templateLiteralTypesPatternsPrefixSuffixAssignability.ts, 2, 5))

const s4: `:${string}:${string}:` = ":::"; // ok
>s4 : Symbol(s4, Decl(templateLiteralTypesPatternsPrefixSuffixAssignability.ts, 3, 5))

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/conformance/types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts ===
const s1: `:${string}:` = ":"; // should error
>s1 : `:${string}:`
>":" : ":"

const s2: `:${string}:` = "::"; // ok
>s2 : `:${string}:`
>"::" : "::"

const s3: `:${string}:${string}:` = "::"; // should error
>s3 : `:${string}:${string}:`
>"::" : "::"

const s4: `:${string}:${string}:` = ":::"; // ok
>s4 : `:${string}:${string}:`
>":::" : ":::"

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const s1: `:${string}:` = ":"; // should error
const s2: `:${string}:` = "::"; // ok
const s3: `:${string}:${string}:` = "::"; // should error
const s4: `:${string}:${string}:` = ":::"; // ok