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

This commit is contained in:
Wesley Wigham
2020-09-29 16:34:58 -07:00
committed by GitHub
parent 2084404b8f
commit 2428ade1a9
6 changed files with 59 additions and 1 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -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

View File

@@ -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))

View File

@@ -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}:`
>":::" : ":::"

View File

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