Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed Dec 9, 2020
1 parent b3cdbf3 commit 5cf670b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 33 deletions.
16 changes: 16 additions & 0 deletions tests/baselines/reference/templateLiteralTypes2.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ function ft13(s: string, cond: boolean) {

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

// Repro from #41631

declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;
Expand Down Expand Up @@ -146,6 +154,13 @@ function ft13(s, cond) {
var y1 = nonWidening("foo" + s);
var y2 = nonWidening(cond ? 'a' : "foo" + s);
}
function ft14(t) {
var x1 = t;
var x2 = t;
var x3 = t;
var x4 = t;
var x6 = t;
}
var t1 = takesLiteral("foo.bar.baz"); // "baz"
var id2 = "foo.bar.baz";
var t2 = takesLiteral(id2); // "baz"
Expand All @@ -169,6 +184,7 @@ declare function widening<T>(x: T): T;
declare function nonWidening<T extends string | number | symbol>(x: T): T;
declare function ft13(s: string, cond: boolean): void;
declare type T0 = string | `${number}px`;
declare function ft14(t: `foo${number}`): void;
declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;
declare const t1: "baz";
declare const id2 = "foo.bar.baz";
Expand Down
94 changes: 61 additions & 33 deletions tests/baselines/reference/templateLiteralTypes2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -229,68 +229,96 @@ function ft13(s: string, cond: boolean) {
type T0 = string | `${number}px`;
>T0 : Symbol(T0, Decl(templateLiteralTypes2.ts, 60, 1))

function ft14(t: `foo${number}`) {
>ft14 : Symbol(ft14, Decl(templateLiteralTypes2.ts, 62, 33))
>t : Symbol(t, Decl(templateLiteralTypes2.ts, 64, 14))

let x1: string = t;
>x1 : Symbol(x1, Decl(templateLiteralTypes2.ts, 65, 7))
>t : Symbol(t, Decl(templateLiteralTypes2.ts, 64, 14))

let x2: String = t;
>x2 : Symbol(x2, Decl(templateLiteralTypes2.ts, 66, 7))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>t : Symbol(t, Decl(templateLiteralTypes2.ts, 64, 14))

let x3: Object = t;
>x3 : Symbol(x3, Decl(templateLiteralTypes2.ts, 67, 7))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>t : Symbol(t, Decl(templateLiteralTypes2.ts, 64, 14))

let x4: {} = t;
>x4 : Symbol(x4, Decl(templateLiteralTypes2.ts, 68, 7))
>t : Symbol(t, Decl(templateLiteralTypes2.ts, 64, 14))

let x6: { length: number } = t;
>x6 : Symbol(x6, Decl(templateLiteralTypes2.ts, 69, 7))
>length : Symbol(length, Decl(templateLiteralTypes2.ts, 69, 13))
>t : Symbol(t, Decl(templateLiteralTypes2.ts, 64, 14))
}

// Repro from #41631

declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 62, 33))
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 66, 30))
>literal : Symbol(literal, Decl(templateLiteralTypes2.ts, 66, 48))
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 66, 30))
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 66, 30))
>R : Symbol(R, Decl(templateLiteralTypes2.ts, 66, 87))
>R : Symbol(R, Decl(templateLiteralTypes2.ts, 66, 87))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 74, 30))
>literal : Symbol(literal, Decl(templateLiteralTypes2.ts, 74, 48))
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 74, 30))
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 74, 30))
>R : Symbol(R, Decl(templateLiteralTypes2.ts, 74, 87))
>R : Symbol(R, Decl(templateLiteralTypes2.ts, 74, 87))

const t1 = takesLiteral("foo.bar.baz"); // "baz"
>t1 : Symbol(t1, Decl(templateLiteralTypes2.ts, 68, 5))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 62, 33))
>t1 : Symbol(t1, Decl(templateLiteralTypes2.ts, 76, 5))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))

const id2 = "foo.bar.baz";
>id2 : Symbol(id2, Decl(templateLiteralTypes2.ts, 69, 5))
>id2 : Symbol(id2, Decl(templateLiteralTypes2.ts, 77, 5))

const t2 = takesLiteral(id2); // "baz"
>t2 : Symbol(t2, Decl(templateLiteralTypes2.ts, 70, 5))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 62, 33))
>id2 : Symbol(id2, Decl(templateLiteralTypes2.ts, 69, 5))
>t2 : Symbol(t2, Decl(templateLiteralTypes2.ts, 78, 5))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
>id2 : Symbol(id2, Decl(templateLiteralTypes2.ts, 77, 5))

declare const someString: string;
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 72, 13))
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 80, 13))

const t3 = takesLiteral(`foo.bar.${someString}`); // string
>t3 : Symbol(t3, Decl(templateLiteralTypes2.ts, 73, 5))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 62, 33))
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 72, 13))
>t3 : Symbol(t3, Decl(templateLiteralTypes2.ts, 81, 5))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 80, 13))

const id4 = `foo.bar.${someString}`;
>id4 : Symbol(id4, Decl(templateLiteralTypes2.ts, 75, 5))
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 72, 13))
>id4 : Symbol(id4, Decl(templateLiteralTypes2.ts, 83, 5))
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 80, 13))

const t4 = takesLiteral(id4); // string
>t4 : Symbol(t4, Decl(templateLiteralTypes2.ts, 76, 5))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 62, 33))
>id4 : Symbol(id4, Decl(templateLiteralTypes2.ts, 75, 5))
>t4 : Symbol(t4, Decl(templateLiteralTypes2.ts, 84, 5))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
>id4 : Symbol(id4, Decl(templateLiteralTypes2.ts, 83, 5))

declare const someUnion: 'abc' | 'def' | 'ghi';
>someUnion : Symbol(someUnion, Decl(templateLiteralTypes2.ts, 78, 13))
>someUnion : Symbol(someUnion, Decl(templateLiteralTypes2.ts, 86, 13))

const t5 = takesLiteral(`foo.bar.${someUnion}`); // "abc" | "def" | "ghi"
>t5 : Symbol(t5, Decl(templateLiteralTypes2.ts, 79, 5))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 62, 33))
>someUnion : Symbol(someUnion, Decl(templateLiteralTypes2.ts, 78, 13))
>t5 : Symbol(t5, Decl(templateLiteralTypes2.ts, 87, 5))
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
>someUnion : Symbol(someUnion, Decl(templateLiteralTypes2.ts, 86, 13))

// Repro from #41732

const pixelValue: number = 22;
>pixelValue : Symbol(pixelValue, Decl(templateLiteralTypes2.ts, 83, 5))
>pixelValue : Symbol(pixelValue, Decl(templateLiteralTypes2.ts, 91, 5))

type PixelValueType = `${number}px`;
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 83, 30))
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 91, 30))

const pixelString: PixelValueType = `22px`;
>pixelString : Symbol(pixelString, Decl(templateLiteralTypes2.ts, 87, 5))
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 83, 30))
>pixelString : Symbol(pixelString, Decl(templateLiteralTypes2.ts, 95, 5))
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 91, 30))

const pixelStringWithTemplate: PixelValueType = `${pixelValue}px`;
>pixelStringWithTemplate : Symbol(pixelStringWithTemplate, Decl(templateLiteralTypes2.ts, 89, 5))
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 83, 30))
>pixelValue : Symbol(pixelValue, Decl(templateLiteralTypes2.ts, 83, 5))
>pixelStringWithTemplate : Symbol(pixelStringWithTemplate, Decl(templateLiteralTypes2.ts, 97, 5))
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 91, 30))
>pixelValue : Symbol(pixelValue, Decl(templateLiteralTypes2.ts, 91, 5))

26 changes: 26 additions & 0 deletions tests/baselines/reference/templateLiteralTypes2.types
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,32 @@ function ft13(s: string, cond: boolean) {
type T0 = string | `${number}px`;
>T0 : string

function ft14(t: `foo${number}`) {
>ft14 : (t: `foo${number}`) => void
>t : `foo${number}`

let x1: string = t;
>x1 : string
>t : `foo${number}`

let x2: String = t;
>x2 : String
>t : `foo${number}`

let x3: Object = t;
>x3 : Object
>t : `foo${number}`

let x4: {} = t;
>x4 : {}
>t : `foo${number}`

let x6: { length: number } = t;
>x6 : { length: number; }
>length : number
>t : `foo${number}`
}

// Repro from #41631

declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ function ft13(s: string, cond: boolean) {

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

// Repro from #41631

declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;
Expand Down

0 comments on commit 5cf670b

Please sign in to comment.