Skip to content

Commit 2428ade

Browse files
authored
Match suffix _after_ prefix when inferring literals out of templates (#40841)
1 parent 2084404 commit 2428ade

6 files changed

+59
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19801,7 +19801,7 @@ namespace ts {
1980119801
const lastIndex = texts.length - 1;
1980219802
const startText = texts[0];
1980319803
const endText = texts[lastIndex];
19804-
if (!(value.startsWith(startText) && value.endsWith(endText))) return undefined;
19804+
if (!(value.startsWith(startText) && value.slice(startText.length).endsWith(endText))) return undefined;
1980519805
const matches = [];
1980619806
const str = value.slice(startText.length, value.length - endText.length);
1980719807
let pos = 0;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/conformance/types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts(1,7): error TS2322: Type '":"' is not assignable to type '`:${string}:`'.
2+
tests/cases/conformance/types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts(3,7): error TS2322: Type '"::"' is not assignable to type '`:${string}:${string}:`'.
3+
4+
5+
==== tests/cases/conformance/types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts (2 errors) ====
6+
const s1: `:${string}:` = ":"; // should error
7+
~~
8+
!!! error TS2322: Type '":"' is not assignable to type '`:${string}:`'.
9+
const s2: `:${string}:` = "::"; // ok
10+
const s3: `:${string}:${string}:` = "::"; // should error
11+
~~
12+
!!! error TS2322: Type '"::"' is not assignable to type '`:${string}:${string}:`'.
13+
const s4: `:${string}:${string}:` = ":::"; // ok
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [templateLiteralTypesPatternsPrefixSuffixAssignability.ts]
2+
const s1: `:${string}:` = ":"; // should error
3+
const s2: `:${string}:` = "::"; // ok
4+
const s3: `:${string}:${string}:` = "::"; // should error
5+
const s4: `:${string}:${string}:` = ":::"; // ok
6+
7+
//// [templateLiteralTypesPatternsPrefixSuffixAssignability.js]
8+
var s1 = ":"; // should error
9+
var s2 = "::"; // ok
10+
var s3 = "::"; // should error
11+
var s4 = ":::"; // ok
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/conformance/types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts ===
2+
const s1: `:${string}:` = ":"; // should error
3+
>s1 : Symbol(s1, Decl(templateLiteralTypesPatternsPrefixSuffixAssignability.ts, 0, 5))
4+
5+
const s2: `:${string}:` = "::"; // ok
6+
>s2 : Symbol(s2, Decl(templateLiteralTypesPatternsPrefixSuffixAssignability.ts, 1, 5))
7+
8+
const s3: `:${string}:${string}:` = "::"; // should error
9+
>s3 : Symbol(s3, Decl(templateLiteralTypesPatternsPrefixSuffixAssignability.ts, 2, 5))
10+
11+
const s4: `:${string}:${string}:` = ":::"; // ok
12+
>s4 : Symbol(s4, Decl(templateLiteralTypesPatternsPrefixSuffixAssignability.ts, 3, 5))
13+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts ===
2+
const s1: `:${string}:` = ":"; // should error
3+
>s1 : `:${string}:`
4+
>":" : ":"
5+
6+
const s2: `:${string}:` = "::"; // ok
7+
>s2 : `:${string}:`
8+
>"::" : "::"
9+
10+
const s3: `:${string}:${string}:` = "::"; // should error
11+
>s3 : `:${string}:${string}:`
12+
>"::" : "::"
13+
14+
const s4: `:${string}:${string}:` = ":::"; // ok
15+
>s4 : `:${string}:${string}:`
16+
>":::" : ":::"
17+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const s1: `:${string}:` = ":"; // should error
2+
const s2: `:${string}:` = "::"; // ok
3+
const s3: `:${string}:${string}:` = "::"; // should error
4+
const s4: `:${string}:${string}:` = ":::"; // ok

0 commit comments

Comments
 (0)