diff --git a/tests/baselines/reference/constAssertions.errors.txt b/tests/baselines/reference/constAssertions.errors.txt new file mode 100644 index 00000000000..24fce159901 --- /dev/null +++ b/tests/baselines/reference/constAssertions.errors.txt @@ -0,0 +1,79 @@ +tests/cases/conformance/expressions/typeAssertions/constAssertions.ts(44,32): error TS2540: Cannot assign to 'x' because it is a read-only property. +tests/cases/conformance/expressions/typeAssertions/constAssertions.ts(61,10): error TS1355: A 'const' assertion can only be applied to a string, number, boolean, array, or object literal. +tests/cases/conformance/expressions/typeAssertions/constAssertions.ts(62,10): error TS1355: A 'const' assertion can only be applied to a string, number, boolean, array, or object literal. +tests/cases/conformance/expressions/typeAssertions/constAssertions.ts(63,10): error TS1355: A 'const' assertion can only be applied to a string, number, boolean, array, or object literal. + + +==== tests/cases/conformance/expressions/typeAssertions/constAssertions.ts (4 errors) ==== + let v1 = 'abc' as const; + let v2 = `abc` as const; + let v3 = 10 as const; + let v4 = -10 as const; + let v5 = +10 as const; + let v6 = 10n as const; + let v7 = -10n as const; + let v8 = true as const; + let v9 = false as const; + + let c1 = 'abc' as const; + let c2 = `abc` as const; + let c3 = 10 as const; + let c4 = -10 as const; + let c5 = +10 as const; + let c6 = 10n as const; + let c7 = -10n as const; + let c8 = true as const; + let c9 = false as const; + + let vv1 = v1; + let vc1 = c1; + + let a1 = [] as const; + let a2 = [1, 2, 3] as const; + let a3 = [10, 'hello', true] as const; + let a4 = [...[1, 2, 3]] as const; + let a5 = [1, 2, 3]; + let a6 = [...a5] as const; + let a7 = [...a6]; + let a8 = ['abc', ...a7] as const; + let a9 = [...a8]; + + declare let d: { [x: string]: string }; + + let o1 = { x: 10, y: 20 } as const; + let o2 = { a: 1, 'b': 2, ['c']: 3, d() {}, ['e' + '']: 4 } as const; + let o3 = { ...o1, ...o2 } as const; + let o4 = { a: 1, b: 2 }; + let o5 = { ...o4 } as const; + let o6 = { ...o5 }; + let o7 = { ...d } as const; + let o8 = { ...o7 }; + let o9 = { x: 10, foo() { this.x = 20 } } as const; // Error + ~ +!!! error TS2540: Cannot assign to 'x' because it is a read-only property. + + let p1 = (10) as const; + let p2 = ((-10)) as const; + let p3 = ([(10)]) as const; + let p4 = [[[[10]]]] as const; + + let x1 = { x: 10, y: [20, 30], z: { a: { b: 42 } } } as const; + + let q1 = 10; + let q2 = 'abc'; + let q3 = true; + let q4 = [1, 2, 3]; + let q5 = { x: 10, y: 20 }; + + declare function id(x: T): T; + + let e1 = v1 as const; // Error + ~~ +!!! error TS1355: A 'const' assertion can only be applied to a string, number, boolean, array, or object literal. + let e2 = (true ? 1 : 0) as const; // Error + ~~~~~~~~~~~~~~ +!!! error TS1355: A 'const' assertion can only be applied to a string, number, boolean, array, or object literal. + let e3 = id(1) as const; // Error + ~~~~~ +!!! error TS1355: A 'const' assertion can only be applied to a string, number, boolean, array, or object literal. + \ No newline at end of file diff --git a/tests/baselines/reference/constAssertions.js b/tests/baselines/reference/constAssertions.js new file mode 100644 index 00000000000..9f173987d22 --- /dev/null +++ b/tests/baselines/reference/constAssertions.js @@ -0,0 +1,220 @@ +//// [constAssertions.ts] +let v1 = 'abc' as const; +let v2 = `abc` as const; +let v3 = 10 as const; +let v4 = -10 as const; +let v5 = +10 as const; +let v6 = 10n as const; +let v7 = -10n as const; +let v8 = true as const; +let v9 = false as const; + +let c1 = 'abc' as const; +let c2 = `abc` as const; +let c3 = 10 as const; +let c4 = -10 as const; +let c5 = +10 as const; +let c6 = 10n as const; +let c7 = -10n as const; +let c8 = true as const; +let c9 = false as const; + +let vv1 = v1; +let vc1 = c1; + +let a1 = [] as const; +let a2 = [1, 2, 3] as const; +let a3 = [10, 'hello', true] as const; +let a4 = [...[1, 2, 3]] as const; +let a5 = [1, 2, 3]; +let a6 = [...a5] as const; +let a7 = [...a6]; +let a8 = ['abc', ...a7] as const; +let a9 = [...a8]; + +declare let d: { [x: string]: string }; + +let o1 = { x: 10, y: 20 } as const; +let o2 = { a: 1, 'b': 2, ['c']: 3, d() {}, ['e' + '']: 4 } as const; +let o3 = { ...o1, ...o2 } as const; +let o4 = { a: 1, b: 2 }; +let o5 = { ...o4 } as const; +let o6 = { ...o5 }; +let o7 = { ...d } as const; +let o8 = { ...o7 }; +let o9 = { x: 10, foo() { this.x = 20 } } as const; // Error + +let p1 = (10) as const; +let p2 = ((-10)) as const; +let p3 = ([(10)]) as const; +let p4 = [[[[10]]]] as const; + +let x1 = { x: 10, y: [20, 30], z: { a: { b: 42 } } } as const; + +let q1 = 10; +let q2 = 'abc'; +let q3 = true; +let q4 = [1, 2, 3]; +let q5 = { x: 10, y: 20 }; + +declare function id(x: T): T; + +let e1 = v1 as const; // Error +let e2 = (true ? 1 : 0) as const; // Error +let e3 = id(1) as const; // Error + + +//// [constAssertions.js] +"use strict"; +let v1 = 'abc'; +let v2 = `abc`; +let v3 = 10; +let v4 = -10; +let v5 = +10; +let v6 = 10n; +let v7 = -10n; +let v8 = true; +let v9 = false; +let c1 = 'abc'; +let c2 = `abc`; +let c3 = 10; +let c4 = -10; +let c5 = +10; +let c6 = 10n; +let c7 = -10n; +let c8 = true; +let c9 = false; +let vv1 = v1; +let vc1 = c1; +let a1 = []; +let a2 = [1, 2, 3]; +let a3 = [10, 'hello', true]; +let a4 = [...[1, 2, 3]]; +let a5 = [1, 2, 3]; +let a6 = [...a5]; +let a7 = [...a6]; +let a8 = ['abc', ...a7]; +let a9 = [...a8]; +let o1 = { x: 10, y: 20 }; +let o2 = { a: 1, 'b': 2, ['c']: 3, d() { }, ['e' + '']: 4 }; +let o3 = { ...o1, ...o2 }; +let o4 = { a: 1, b: 2 }; +let o5 = { ...o4 }; +let o6 = { ...o5 }; +let o7 = { ...d }; +let o8 = { ...o7 }; +let o9 = { x: 10, foo() { this.x = 20; } }; // Error +let p1 = (10); +let p2 = ((-10)); +let p3 = ([(10)]); +let p4 = [[[[10]]]]; +let x1 = { x: 10, y: [20, 30], z: { a: { b: 42 } } }; +let q1 = 10; +let q2 = 'abc'; +let q3 = true; +let q4 = [1, 2, 3]; +let q5 = { x: 10, y: 20 }; +let e1 = v1; // Error +let e2 = (true ? 1 : 0); // Error +let e3 = id(1); // Error + + +//// [constAssertions.d.ts] +declare let v1: "abc"; +declare let v2: "abc"; +declare let v3: 10; +declare let v4: -10; +declare let v5: 10; +declare let v6: 10n; +declare let v7: -10n; +declare let v8: true; +declare let v9: false; +declare let c1: "abc"; +declare let c2: "abc"; +declare let c3: 10; +declare let c4: -10; +declare let c5: 10; +declare let c6: 10n; +declare let c7: -10n; +declare let c8: true; +declare let c9: false; +declare let vv1: "abc"; +declare let vc1: "abc"; +declare let a1: readonly []; +declare let a2: readonly [1, 2, 3]; +declare let a3: readonly [10, "hello", true]; +declare let a4: readonly (1 | 2 | 3)[]; +declare let a5: number[]; +declare let a6: readonly number[]; +declare let a7: number[]; +declare let a8: readonly ["abc", ...number[]]; +declare let a9: (number | "abc")[]; +declare let d: { + [x: string]: string; +}; +declare let o1: { + readonly x: 10; + readonly y: 20; +}; +declare let o2: { + readonly [x: string]: 1 | 2 | 3 | (() => void) | 4; + readonly a: 1; + readonly 'b': 2; + readonly ['c']: 3; + readonly d: () => void; +}; +declare let o3: { + readonly a: 1; + readonly 'b': 2; + readonly ['c']: 3; + readonly d: () => void; + readonly x: 10; + readonly y: 20; +}; +declare let o4: { + a: number; + b: number; +}; +declare let o5: { + readonly a: number; + readonly b: number; +}; +declare let o6: { + a: number; + b: number; +}; +declare let o7: { + readonly [x: string]: string; +}; +declare let o8: { + [x: string]: string; +}; +declare let o9: { + readonly x: 10; + readonly foo: () => void; +}; +declare let p1: 10; +declare let p2: -10; +declare let p3: readonly [10]; +declare let p4: readonly [readonly [readonly [readonly [10]]]]; +declare let x1: { + readonly x: 10; + readonly y: readonly [20, 30]; + z: { + a: { + readonly b: 42; + }; + }; +}; +declare let q1: 10; +declare let q2: "abc"; +declare let q3: true; +declare let q4: readonly [1, 2, 3]; +declare let q5: { + readonly x: 10; + readonly y: 20; +}; +declare function id(x: T): T; +declare let e1: "abc"; +declare let e2: 0 | 1; +declare let e3: 1; diff --git a/tests/baselines/reference/constAssertions.symbols b/tests/baselines/reference/constAssertions.symbols new file mode 100644 index 00000000000..598210f42b3 --- /dev/null +++ b/tests/baselines/reference/constAssertions.symbols @@ -0,0 +1,201 @@ +=== tests/cases/conformance/expressions/typeAssertions/constAssertions.ts === +let v1 = 'abc' as const; +>v1 : Symbol(v1, Decl(constAssertions.ts, 0, 3)) + +let v2 = `abc` as const; +>v2 : Symbol(v2, Decl(constAssertions.ts, 1, 3)) + +let v3 = 10 as const; +>v3 : Symbol(v3, Decl(constAssertions.ts, 2, 3)) + +let v4 = -10 as const; +>v4 : Symbol(v4, Decl(constAssertions.ts, 3, 3)) + +let v5 = +10 as const; +>v5 : Symbol(v5, Decl(constAssertions.ts, 4, 3)) + +let v6 = 10n as const; +>v6 : Symbol(v6, Decl(constAssertions.ts, 5, 3)) + +let v7 = -10n as const; +>v7 : Symbol(v7, Decl(constAssertions.ts, 6, 3)) + +let v8 = true as const; +>v8 : Symbol(v8, Decl(constAssertions.ts, 7, 3)) + +let v9 = false as const; +>v9 : Symbol(v9, Decl(constAssertions.ts, 8, 3)) + +let c1 = 'abc' as const; +>c1 : Symbol(c1, Decl(constAssertions.ts, 10, 3)) + +let c2 = `abc` as const; +>c2 : Symbol(c2, Decl(constAssertions.ts, 11, 3)) + +let c3 = 10 as const; +>c3 : Symbol(c3, Decl(constAssertions.ts, 12, 3)) + +let c4 = -10 as const; +>c4 : Symbol(c4, Decl(constAssertions.ts, 13, 3)) + +let c5 = +10 as const; +>c5 : Symbol(c5, Decl(constAssertions.ts, 14, 3)) + +let c6 = 10n as const; +>c6 : Symbol(c6, Decl(constAssertions.ts, 15, 3)) + +let c7 = -10n as const; +>c7 : Symbol(c7, Decl(constAssertions.ts, 16, 3)) + +let c8 = true as const; +>c8 : Symbol(c8, Decl(constAssertions.ts, 17, 3)) + +let c9 = false as const; +>c9 : Symbol(c9, Decl(constAssertions.ts, 18, 3)) + +let vv1 = v1; +>vv1 : Symbol(vv1, Decl(constAssertions.ts, 20, 3)) +>v1 : Symbol(v1, Decl(constAssertions.ts, 0, 3)) + +let vc1 = c1; +>vc1 : Symbol(vc1, Decl(constAssertions.ts, 21, 3)) +>c1 : Symbol(c1, Decl(constAssertions.ts, 10, 3)) + +let a1 = [] as const; +>a1 : Symbol(a1, Decl(constAssertions.ts, 23, 3)) + +let a2 = [1, 2, 3] as const; +>a2 : Symbol(a2, Decl(constAssertions.ts, 24, 3)) + +let a3 = [10, 'hello', true] as const; +>a3 : Symbol(a3, Decl(constAssertions.ts, 25, 3)) + +let a4 = [...[1, 2, 3]] as const; +>a4 : Symbol(a4, Decl(constAssertions.ts, 26, 3)) + +let a5 = [1, 2, 3]; +>a5 : Symbol(a5, Decl(constAssertions.ts, 27, 3)) + +let a6 = [...a5] as const; +>a6 : Symbol(a6, Decl(constAssertions.ts, 28, 3)) +>a5 : Symbol(a5, Decl(constAssertions.ts, 27, 3)) + +let a7 = [...a6]; +>a7 : Symbol(a7, Decl(constAssertions.ts, 29, 3)) +>a6 : Symbol(a6, Decl(constAssertions.ts, 28, 3)) + +let a8 = ['abc', ...a7] as const; +>a8 : Symbol(a8, Decl(constAssertions.ts, 30, 3)) +>a7 : Symbol(a7, Decl(constAssertions.ts, 29, 3)) + +let a9 = [...a8]; +>a9 : Symbol(a9, Decl(constAssertions.ts, 31, 3)) +>a8 : Symbol(a8, Decl(constAssertions.ts, 30, 3)) + +declare let d: { [x: string]: string }; +>d : Symbol(d, Decl(constAssertions.ts, 33, 11)) +>x : Symbol(x, Decl(constAssertions.ts, 33, 18)) + +let o1 = { x: 10, y: 20 } as const; +>o1 : Symbol(o1, Decl(constAssertions.ts, 35, 3)) +>x : Symbol(x, Decl(constAssertions.ts, 35, 10)) +>y : Symbol(y, Decl(constAssertions.ts, 35, 17)) + +let o2 = { a: 1, 'b': 2, ['c']: 3, d() {}, ['e' + '']: 4 } as const; +>o2 : Symbol(o2, Decl(constAssertions.ts, 36, 3)) +>a : Symbol(a, Decl(constAssertions.ts, 36, 10)) +>'b' : Symbol('b', Decl(constAssertions.ts, 36, 16)) +>['c'] : Symbol(['c'], Decl(constAssertions.ts, 36, 24)) +>'c' : Symbol(['c'], Decl(constAssertions.ts, 36, 24)) +>d : Symbol(d, Decl(constAssertions.ts, 36, 34)) +>['e' + ''] : Symbol(['e' + ''], Decl(constAssertions.ts, 36, 42)) + +let o3 = { ...o1, ...o2 } as const; +>o3 : Symbol(o3, Decl(constAssertions.ts, 37, 3)) +>o1 : Symbol(o1, Decl(constAssertions.ts, 35, 3)) +>o2 : Symbol(o2, Decl(constAssertions.ts, 36, 3)) + +let o4 = { a: 1, b: 2 }; +>o4 : Symbol(o4, Decl(constAssertions.ts, 38, 3)) +>a : Symbol(a, Decl(constAssertions.ts, 38, 10)) +>b : Symbol(b, Decl(constAssertions.ts, 38, 16)) + +let o5 = { ...o4 } as const; +>o5 : Symbol(o5, Decl(constAssertions.ts, 39, 3)) +>o4 : Symbol(o4, Decl(constAssertions.ts, 38, 3)) + +let o6 = { ...o5 }; +>o6 : Symbol(o6, Decl(constAssertions.ts, 40, 3)) +>o5 : Symbol(o5, Decl(constAssertions.ts, 39, 3)) + +let o7 = { ...d } as const; +>o7 : Symbol(o7, Decl(constAssertions.ts, 41, 3)) +>d : Symbol(d, Decl(constAssertions.ts, 33, 11)) + +let o8 = { ...o7 }; +>o8 : Symbol(o8, Decl(constAssertions.ts, 42, 3)) +>o7 : Symbol(o7, Decl(constAssertions.ts, 41, 3)) + +let o9 = { x: 10, foo() { this.x = 20 } } as const; // Error +>o9 : Symbol(o9, Decl(constAssertions.ts, 43, 3)) +>x : Symbol(x, Decl(constAssertions.ts, 43, 10)) +>foo : Symbol(foo, Decl(constAssertions.ts, 43, 17)) +>this.x : Symbol(x, Decl(constAssertions.ts, 43, 10)) +>this : Symbol(__object, Decl(constAssertions.ts, 43, 8)) +>x : Symbol(x, Decl(constAssertions.ts, 43, 10)) + +let p1 = (10) as const; +>p1 : Symbol(p1, Decl(constAssertions.ts, 45, 3)) + +let p2 = ((-10)) as const; +>p2 : Symbol(p2, Decl(constAssertions.ts, 46, 3)) + +let p3 = ([(10)]) as const; +>p3 : Symbol(p3, Decl(constAssertions.ts, 47, 3)) + +let p4 = [[[[10]]]] as const; +>p4 : Symbol(p4, Decl(constAssertions.ts, 48, 3)) + +let x1 = { x: 10, y: [20, 30], z: { a: { b: 42 } } } as const; +>x1 : Symbol(x1, Decl(constAssertions.ts, 50, 3)) +>x : Symbol(x, Decl(constAssertions.ts, 50, 10)) +>y : Symbol(y, Decl(constAssertions.ts, 50, 17)) +>z : Symbol(z, Decl(constAssertions.ts, 50, 30)) +>a : Symbol(a, Decl(constAssertions.ts, 50, 35)) +>b : Symbol(b, Decl(constAssertions.ts, 50, 40)) + +let q1 = 10; +>q1 : Symbol(q1, Decl(constAssertions.ts, 52, 3)) + +let q2 = 'abc'; +>q2 : Symbol(q2, Decl(constAssertions.ts, 53, 3)) + +let q3 = true; +>q3 : Symbol(q3, Decl(constAssertions.ts, 54, 3)) + +let q4 = [1, 2, 3]; +>q4 : Symbol(q4, Decl(constAssertions.ts, 55, 3)) + +let q5 = { x: 10, y: 20 }; +>q5 : Symbol(q5, Decl(constAssertions.ts, 56, 3)) +>x : Symbol(x, Decl(constAssertions.ts, 56, 18)) +>y : Symbol(y, Decl(constAssertions.ts, 56, 25)) + +declare function id(x: T): T; +>id : Symbol(id, Decl(constAssertions.ts, 56, 34)) +>T : Symbol(T, Decl(constAssertions.ts, 58, 20)) +>x : Symbol(x, Decl(constAssertions.ts, 58, 23)) +>T : Symbol(T, Decl(constAssertions.ts, 58, 20)) +>T : Symbol(T, Decl(constAssertions.ts, 58, 20)) + +let e1 = v1 as const; // Error +>e1 : Symbol(e1, Decl(constAssertions.ts, 60, 3)) +>v1 : Symbol(v1, Decl(constAssertions.ts, 0, 3)) + +let e2 = (true ? 1 : 0) as const; // Error +>e2 : Symbol(e2, Decl(constAssertions.ts, 61, 3)) + +let e3 = id(1) as const; // Error +>e3 : Symbol(e3, Decl(constAssertions.ts, 62, 3)) +>id : Symbol(id, Decl(constAssertions.ts, 56, 34)) + diff --git a/tests/baselines/reference/constAssertions.types b/tests/baselines/reference/constAssertions.types new file mode 100644 index 00000000000..b1f90df15f0 --- /dev/null +++ b/tests/baselines/reference/constAssertions.types @@ -0,0 +1,356 @@ +=== tests/cases/conformance/expressions/typeAssertions/constAssertions.ts === +let v1 = 'abc' as const; +>v1 : "abc" +>'abc' as const : "abc" +>'abc' : "abc" + +let v2 = `abc` as const; +>v2 : "abc" +>`abc` as const : "abc" +>`abc` : "abc" + +let v3 = 10 as const; +>v3 : 10 +>10 as const : 10 +>10 : 10 + +let v4 = -10 as const; +>v4 : -10 +>-10 as const : -10 +>-10 : -10 +>10 : 10 + +let v5 = +10 as const; +>v5 : 10 +>+10 as const : 10 +>+10 : 10 +>10 : 10 + +let v6 = 10n as const; +>v6 : 10n +>10n as const : 10n +>10n : 10n + +let v7 = -10n as const; +>v7 : -10n +>-10n as const : -10n +>-10n : -10n +>10n : 10n + +let v8 = true as const; +>v8 : true +>true as const : true +>true : true + +let v9 = false as const; +>v9 : false +>false as const : false +>false : false + +let c1 = 'abc' as const; +>c1 : "abc" +>'abc' as const : "abc" +>'abc' : "abc" + +let c2 = `abc` as const; +>c2 : "abc" +>`abc` as const : "abc" +>`abc` : "abc" + +let c3 = 10 as const; +>c3 : 10 +>10 as const : 10 +>10 : 10 + +let c4 = -10 as const; +>c4 : -10 +>-10 as const : -10 +>-10 : -10 +>10 : 10 + +let c5 = +10 as const; +>c5 : 10 +>+10 as const : 10 +>+10 : 10 +>10 : 10 + +let c6 = 10n as const; +>c6 : 10n +>10n as const : 10n +>10n : 10n + +let c7 = -10n as const; +>c7 : -10n +>-10n as const : -10n +>-10n : -10n +>10n : 10n + +let c8 = true as const; +>c8 : true +>true as const : true +>true : true + +let c9 = false as const; +>c9 : false +>false as const : false +>false : false + +let vv1 = v1; +>vv1 : "abc" +>v1 : "abc" + +let vc1 = c1; +>vc1 : "abc" +>c1 : "abc" + +let a1 = [] as const; +>a1 : readonly [] +>[] as const : readonly [] +>[] : readonly [] + +let a2 = [1, 2, 3] as const; +>a2 : readonly [1, 2, 3] +>[1, 2, 3] as const : readonly [1, 2, 3] +>[1, 2, 3] : readonly [1, 2, 3] +>1 : 1 +>2 : 2 +>3 : 3 + +let a3 = [10, 'hello', true] as const; +>a3 : readonly [10, "hello", true] +>[10, 'hello', true] as const : readonly [10, "hello", true] +>[10, 'hello', true] : readonly [10, "hello", true] +>10 : 10 +>'hello' : "hello" +>true : true + +let a4 = [...[1, 2, 3]] as const; +>a4 : readonly (1 | 2 | 3)[] +>[...[1, 2, 3]] as const : readonly (1 | 2 | 3)[] +>[...[1, 2, 3]] : readonly (1 | 2 | 3)[] +>...[1, 2, 3] : 1 | 2 | 3 +>[1, 2, 3] : readonly [1, 2, 3] +>1 : 1 +>2 : 2 +>3 : 3 + +let a5 = [1, 2, 3]; +>a5 : number[] +>[1, 2, 3] : number[] +>1 : 1 +>2 : 2 +>3 : 3 + +let a6 = [...a5] as const; +>a6 : readonly number[] +>[...a5] as const : readonly number[] +>[...a5] : readonly number[] +>...a5 : number +>a5 : number[] + +let a7 = [...a6]; +>a7 : number[] +>[...a6] : number[] +>...a6 : number +>a6 : readonly number[] + +let a8 = ['abc', ...a7] as const; +>a8 : readonly ["abc", ...number[]] +>['abc', ...a7] as const : readonly ["abc", ...number[]] +>['abc', ...a7] : readonly ["abc", ...number[]] +>'abc' : "abc" +>...a7 : number +>a7 : number[] + +let a9 = [...a8]; +>a9 : (number | "abc")[] +>[...a8] : (number | "abc")[] +>...a8 : number | "abc" +>a8 : readonly ["abc", ...number[]] + +declare let d: { [x: string]: string }; +>d : { [x: string]: string; } +>x : string + +let o1 = { x: 10, y: 20 } as const; +>o1 : { readonly x: 10; readonly y: 20; } +>{ x: 10, y: 20 } as const : { readonly x: 10; readonly y: 20; } +>{ x: 10, y: 20 } : { readonly x: 10; readonly y: 20; } +>x : 10 +>10 : 10 +>y : 20 +>20 : 20 + +let o2 = { a: 1, 'b': 2, ['c']: 3, d() {}, ['e' + '']: 4 } as const; +>o2 : { readonly [x: string]: 1 | 2 | 3 | (() => void) | 4; readonly a: 1; readonly 'b': 2; readonly ['c']: 3; readonly d: () => void; } +>{ a: 1, 'b': 2, ['c']: 3, d() {}, ['e' + '']: 4 } as const : { readonly [x: string]: 1 | 2 | 3 | (() => void) | 4; readonly a: 1; readonly 'b': 2; readonly ['c']: 3; readonly d: () => void; } +>{ a: 1, 'b': 2, ['c']: 3, d() {}, ['e' + '']: 4 } : { readonly [x: string]: 1 | 2 | 3 | (() => void) | 4; readonly a: 1; readonly 'b': 2; readonly ['c']: 3; readonly d: () => void; } +>a : 1 +>1 : 1 +>'b' : 2 +>2 : 2 +>['c'] : 3 +>'c' : "c" +>3 : 3 +>d : () => void +>['e' + ''] : 4 +>'e' + '' : string +>'e' : "e" +>'' : "" +>4 : 4 + +let o3 = { ...o1, ...o2 } as const; +>o3 : { readonly a: 1; readonly 'b': 2; readonly ['c']: 3; readonly d: () => void; readonly x: 10; readonly y: 20; } +>{ ...o1, ...o2 } as const : { readonly a: 1; readonly 'b': 2; readonly ['c']: 3; readonly d: () => void; readonly x: 10; readonly y: 20; } +>{ ...o1, ...o2 } : { readonly a: 1; readonly 'b': 2; readonly ['c']: 3; readonly d: () => void; readonly x: 10; readonly y: 20; } +>o1 : { readonly x: 10; readonly y: 20; } +>o2 : { readonly [x: string]: 1 | 2 | 3 | (() => void) | 4; readonly a: 1; readonly 'b': 2; readonly ['c']: 3; readonly d: () => void; } + +let o4 = { a: 1, b: 2 }; +>o4 : { a: number; b: number; } +>{ a: 1, b: 2 } : { a: number; b: number; } +>a : number +>1 : 1 +>b : number +>2 : 2 + +let o5 = { ...o4 } as const; +>o5 : { readonly a: number; readonly b: number; } +>{ ...o4 } as const : { readonly a: number; readonly b: number; } +>{ ...o4 } : { readonly a: number; readonly b: number; } +>o4 : { a: number; b: number; } + +let o6 = { ...o5 }; +>o6 : { a: number; b: number; } +>{ ...o5 } : { a: number; b: number; } +>o5 : { readonly a: number; readonly b: number; } + +let o7 = { ...d } as const; +>o7 : { readonly [x: string]: string; } +>{ ...d } as const : { readonly [x: string]: string; } +>{ ...d } : { readonly [x: string]: string; } +>d : { [x: string]: string; } + +let o8 = { ...o7 }; +>o8 : { [x: string]: string; } +>{ ...o7 } : { [x: string]: string; } +>o7 : { readonly [x: string]: string; } + +let o9 = { x: 10, foo() { this.x = 20 } } as const; // Error +>o9 : { readonly x: 10; readonly foo: () => void; } +>{ x: 10, foo() { this.x = 20 } } as const : { readonly x: 10; readonly foo: () => void; } +>{ x: 10, foo() { this.x = 20 } } : { readonly x: 10; readonly foo: () => void; } +>x : 10 +>10 : 10 +>foo : () => void +>this.x = 20 : 20 +>this.x : any +>this : { readonly x: 10; readonly foo: () => void; } +>x : any +>20 : 20 + +let p1 = (10) as const; +>p1 : 10 +>(10) as const : 10 +>(10) : 10 +>10 : 10 + +let p2 = ((-10)) as const; +>p2 : -10 +>((-10)) as const : -10 +>((-10)) : -10 +>(-10) : -10 +>-10 : -10 +>10 : 10 + +let p3 = ([(10)]) as const; +>p3 : readonly [10] +>([(10)]) as const : readonly [10] +>([(10)]) : readonly [10] +>[(10)] : readonly [10] +>(10) : 10 +>10 : 10 + +let p4 = [[[[10]]]] as const; +>p4 : readonly [readonly [readonly [readonly [10]]]] +>[[[[10]]]] as const : readonly [readonly [readonly [readonly [10]]]] +>[[[[10]]]] : readonly [readonly [readonly [readonly [10]]]] +>[[[10]]] : readonly [readonly [readonly [10]]] +>[[10]] : readonly [readonly [10]] +>[10] : readonly [10] +>10 : 10 + +let x1 = { x: 10, y: [20, 30], z: { a: { b: 42 } } } as const; +>x1 : { readonly x: 10; readonly y: readonly [20, 30]; z: { a: { readonly b: 42; }; }; } +>{ x: 10, y: [20, 30], z: { a: { b: 42 } } } as const : { readonly x: 10; readonly y: readonly [20, 30]; readonly z: { readonly a: { readonly b: 42; }; }; } +>{ x: 10, y: [20, 30], z: { a: { b: 42 } } } : { readonly x: 10; readonly y: readonly [20, 30]; readonly z: { readonly a: { readonly b: 42; }; }; } +>x : 10 +>10 : 10 +>y : readonly [20, 30] +>[20, 30] : readonly [20, 30] +>20 : 20 +>30 : 30 +>z : { readonly a: { readonly b: 42; }; } +>{ a: { b: 42 } } : { readonly a: { readonly b: 42; }; } +>a : { readonly b: 42; } +>{ b: 42 } : { readonly b: 42; } +>b : 42 +>42 : 42 + +let q1 = 10; +>q1 : 10 +> 10 : 10 +>10 : 10 + +let q2 = 'abc'; +>q2 : "abc" +> 'abc' : "abc" +>'abc' : "abc" + +let q3 = true; +>q3 : true +> true : true +>true : true + +let q4 = [1, 2, 3]; +>q4 : readonly [1, 2, 3] +> [1, 2, 3] : readonly [1, 2, 3] +>[1, 2, 3] : readonly [1, 2, 3] +>1 : 1 +>2 : 2 +>3 : 3 + +let q5 = { x: 10, y: 20 }; +>q5 : { readonly x: 10; readonly y: 20; } +> { x: 10, y: 20 } : { readonly x: 10; readonly y: 20; } +>{ x: 10, y: 20 } : { readonly x: 10; readonly y: 20; } +>x : 10 +>10 : 10 +>y : 20 +>20 : 20 + +declare function id(x: T): T; +>id : (x: T) => T +>x : T + +let e1 = v1 as const; // Error +>e1 : "abc" +>v1 as const : "abc" +>v1 : "abc" + +let e2 = (true ? 1 : 0) as const; // Error +>e2 : 0 | 1 +>(true ? 1 : 0) as const : 0 | 1 +>(true ? 1 : 0) : 0 | 1 +>true ? 1 : 0 : 0 | 1 +>true : true +>1 : 1 +>0 : 0 + +let e3 = id(1) as const; // Error +>e3 : 1 +>id(1) as const : 1 +>id(1) : 1 +>id : (x: T) => T +>1 : 1 +