1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
tests/cases/conformance/types/literal/templateLiteralTypes2.ts(23,11): error TS2322: Type 'string' is not assignable to type '`abc${string}`'.
tests/cases/conformance/types/literal/templateLiteralTypes2.ts(29,11): error TS2322: Type 'string' is not assignable to type '`foo${string}` | `bar${string}`'.
tests/cases/conformance/types/literal/templateLiteralTypes2.ts(32,11): error TS2322: Type 'string' is not assignable to type '`foo${string}` | `bar${string}` | `baz${string}`'.
==== tests/cases/conformance/types/literal/templateLiteralTypes2.ts (3 errors) ====
function ft1<T extends string>(s: string, n: number, u: 'foo' | 'bar' | 'baz', t: T) {
const c1 = `abc${s}`;
const c2 = `abc${n}`;
const c3 = `abc${u}`;
const c4 = `abc${t}`;
const d1: `abc${string}` = `abc${s}`;
const d2: `abc${number}` = `abc${n}`;
const d3: `abc${'foo' | 'bar' | 'baz'}` = `abc${u}`;
const d4: `abc${T}` = `abc${t}`;
}
function ft2(s: string) {
return `abc${s}`;
}
function ft10(s: string) {
const c1 = `abc${s}`; // Type string
let v1 = c1; // Type string
const c2 = c1; // Type string
let v2 = c2; // Type string
const c3: `abc${string}` = `abc${s}`;
let v3 = c3; // Type `abc${string}`
const c4: `abc${string}` = c1; // Type `abc${string}`
~~
!!! error TS2322: Type 'string' is not assignable to type '`abc${string}`'.
let v4 = c4; // Type `abc${string}`
}
function ft11(s: string, cond: boolean) {
const c1 = cond ? `foo${s}` : `bar${s}`; // string
const c2: `foo${string}` | `bar${string}` = c1; // `foo${string}` | `bar${string}`
~~
!!! error TS2322: Type 'string' is not assignable to type '`foo${string}` | `bar${string}`'.
const c3 = cond ? c1 : c2; // string
const c4 = cond ? c3 : `baz${s}`; // string
const c5: `foo${string}` | `bar${string}` | `baz${string}` = c4; // `foo${string}` | `bar${string}` | `baz${string}`
~~
!!! error TS2322: Type 'string' is not assignable to type '`foo${string}` | `bar${string}` | `baz${string}`'.
let v1 = c1; // string
let v2 = c2; // `foo${string}` | `bar${string}`
let v3 = c3; // string
let v4 = c4; // string
let v5 = c5; // `foo${string}` | `bar${string}` | `baz${string}`
}
function ft12(s: string) {
const c1 = `foo${s}`;
let v1 = c1;
const c2: `foo${string}` = `foo${s}`;
let v2 = c2;
const c3 = `foo${s}` as `foo${string}`;
let v3 = c3;
const c4 = <`foo${string}`>`foo${s}`;
let v4 = c4;
const c5 = `foo${s}` as const;
let v5 = c5;
}
declare function widening<T>(x: T): T;
declare function nonWidening<T extends string | number | symbol>(x: T): T;
function ft13(s: string, cond: boolean) {
let x1 = widening(`foo${s}`);
let x2 = widening(cond ? 'a' : `foo${s}`);
let y1 = nonWidening(`foo${s}`);
let y2 = nonWidening(cond ? 'a' : `foo${s}`);
}
type T0 = string | `${number}px`;
function ft14(t: `foo${number}`) {
let x1: string = t;
let x2: String = t;
let x3: Object = t;
let x4: {} = t;
let x6: { length: number } = t;
}
declare function g1<T>(x: T): T;
declare function g2<T extends string>(x: T): T;
function ft20(s: string) {
let x1 = g1(`xyz-${s}`); // string
let x2 = g2(`xyz-${s}`); // `xyz-${string}`
}
// Repro from #41631
declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;
const t1 = takesLiteral("foo.bar.baz"); // "baz"
const id2 = "foo.bar.baz";
const t2 = takesLiteral(id2); // "baz"
declare const someString: string;
const t3 = takesLiteral(`foo.bar.${someString}`); // string
const id4 = `foo.bar.${someString}`;
const t4 = takesLiteral(id4); // unknown
declare const someUnion: 'abc' | 'def' | 'ghi';
const t5 = takesLiteral(`foo.bar.${someUnion}`); // "abc" | "def" | "ghi"
// Repro from #41732
const pixelValue: number = 22;
type PixelValueType = `${number}px`;
const pixelString: PixelValueType = `22px`;
const pixelStringWithTemplate: PixelValueType = `${pixelValue}px`;
// Repro from #43143
function getCardTitle(title: string): `test-${string}` {
return `test-${title}`;
}
// Repro from #43424
const interpolatedStyle = { rotate: 12 };
function C2(transform: "-moz-initial" | (string & {})) { return 12; }
C2(`rotate(${interpolatedStyle.rotate}dig)`);
|