--- source: crates/biome_js_analyze/tests/spec_tests.rs expression: invalid.ts --- # Input ```ts // ref: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts let foo = { bar: 'baz' as 'baz' }; let foo = { bar: 1 as 1 }; let []: 'bar' = 'bar'; let foo: 'bar' = 'bar'; let foo: 2 = 2; let foo: 'bar' = 'bar' as 'bar'; let foo = <'bar'>'bar'; let foo = <4>4; let foo = 'bar' as 'bar'; let foo = 5 as 5; class foo { bar: 'baz' = 'baz'; } class foo { bar: 2 = 2; } class foo { foo = <'bar'>'bar'; } class foo { foo = 'bar' as 'bar'; } class foo { foo = 5 as 5; } class foo { constructor(protected prop = 1 as 1) {} } function foo(param = 1 as 1) {} class foo { constructor(protected prop = 'bar' as 'bar') {} } function foo(param = 'bar' as 'bar') {} ``` # Diagnostics ``` invalid.ts:3:27 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 1 │ // ref: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts 2 │ > 3 │ let foo = { bar: 'baz' as 'baz' }; │ ^^^^^ 4 │ let foo = { bar: 1 as 1 }; 5 │ let []: 'bar' = 'bar'; i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 1 1 │ // ref: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts 2 2 │ 3 │ - let·foo·=·{·bar:·'baz'·as·'baz'·}; 3 │ + let·foo·=·{·bar:·'baz'·as·const·}; 4 4 │ let foo = { bar: 1 as 1 }; 5 5 │ let []: 'bar' = 'bar'; ``` ``` invalid.ts:4:23 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 3 │ let foo = { bar: 'baz' as 'baz' }; > 4 │ let foo = { bar: 1 as 1 }; │ ^ 5 │ let []: 'bar' = 'bar'; 6 │ let foo: 'bar' = 'bar'; i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 2 2 │ 3 3 │ let foo = { bar: 'baz' as 'baz' }; 4 │ - let·foo·=·{·bar:·1·as·1·}; 4 │ + let·foo·=·{·bar:·1·as·const·}; 5 5 │ let []: 'bar' = 'bar'; 6 6 │ let foo: 'bar' = 'bar'; ``` ``` invalid.ts:5:9 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of type annotation. 3 │ let foo = { bar: 'baz' as 'baz' }; 4 │ let foo = { bar: 1 as 1 }; > 5 │ let []: 'bar' = 'bar'; │ ^^^^^ 6 │ let foo: 'bar' = 'bar'; 7 │ let foo: 2 = 2; i as const doesn't require any update when the value is changed. i Safe fix: Replace with as const. 3 3 │ let foo = { bar: 'baz' as 'baz' }; 4 4 │ let foo = { bar: 1 as 1 }; 5 │ - let·[]:·'bar'·=·'bar'; 5 │ + let·[]·=·'bar'·as·const; 6 6 │ let foo: 'bar' = 'bar'; 7 7 │ let foo: 2 = 2; ``` ``` invalid.ts:6:10 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of type annotation. 4 │ let foo = { bar: 1 as 1 }; 5 │ let []: 'bar' = 'bar'; > 6 │ let foo: 'bar' = 'bar'; │ ^^^^^ 7 │ let foo: 2 = 2; 8 │ let foo: 'bar' = 'bar' as 'bar'; i as const doesn't require any update when the value is changed. i Safe fix: Replace with as const. 4 4 │ let foo = { bar: 1 as 1 }; 5 5 │ let []: 'bar' = 'bar'; 6 │ - let·foo:·'bar'·=·'bar'; 6 │ + let·foo·=·'bar'·as·const; 7 7 │ let foo: 2 = 2; 8 8 │ let foo: 'bar' = 'bar' as 'bar'; ``` ``` invalid.ts:7:10 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of type annotation. 5 │ let []: 'bar' = 'bar'; 6 │ let foo: 'bar' = 'bar'; > 7 │ let foo: 2 = 2; │ ^ 8 │ let foo: 'bar' = 'bar' as 'bar'; 9 │ let foo = <'bar'>'bar'; i as const doesn't require any update when the value is changed. i Safe fix: Replace with as const. 5 5 │ let []: 'bar' = 'bar'; 6 6 │ let foo: 'bar' = 'bar'; 7 │ - let·foo:·2·=·2; 7 │ + let·foo·=·2·as·const; 8 8 │ let foo: 'bar' = 'bar' as 'bar'; 9 9 │ let foo = <'bar'>'bar'; ``` ``` invalid.ts:8:27 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 6 │ let foo: 'bar' = 'bar'; 7 │ let foo: 2 = 2; > 8 │ let foo: 'bar' = 'bar' as 'bar'; │ ^^^^^ 9 │ let foo = <'bar'>'bar'; 10 │ let foo = <4>4; i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 6 6 │ let foo: 'bar' = 'bar'; 7 7 │ let foo: 2 = 2; 8 │ - let·foo:·'bar'·=·'bar'·as·'bar'; 8 │ + let·foo:·'bar'·=·'bar'·as·const; 9 9 │ let foo = <'bar'>'bar'; 10 10 │ let foo = <4>4; ``` ``` invalid.ts:9:12 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of angle bracket type assertion. 7 │ let foo: 2 = 2; 8 │ let foo: 'bar' = 'bar' as 'bar'; > 9 │ let foo = <'bar'>'bar'; │ ^^^^^ 10 │ let foo = <4>4; 11 │ let foo = 'bar' as 'bar'; i The angle bracket assertion can occasionally be confused with JSX syntax, so using the as const is a preferable alternative. i Safe fix: Replace with as const. 7 7 │ let foo: 2 = 2; 8 8 │ let foo: 'bar' = 'bar' as 'bar'; 9 │ - let·foo·=·<'bar'>'bar'; 9 │ + let·foo·=·'bar'·as·const; 10 10 │ let foo = <4>4; 11 11 │ let foo = 'bar' as 'bar'; ``` ``` invalid.ts:10:12 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of angle bracket type assertion. 8 │ let foo: 'bar' = 'bar' as 'bar'; 9 │ let foo = <'bar'>'bar'; > 10 │ let foo = <4>4; │ ^ 11 │ let foo = 'bar' as 'bar'; 12 │ let foo = 5 as 5; i The angle bracket assertion can occasionally be confused with JSX syntax, so using the as const is a preferable alternative. i Safe fix: Replace with as const. 8 8 │ let foo: 'bar' = 'bar' as 'bar'; 9 9 │ let foo = <'bar'>'bar'; 10 │ - let·foo·=·<4>4; 10 │ + let·foo·=·4·as·const; 11 11 │ let foo = 'bar' as 'bar'; 12 12 │ let foo = 5 as 5; ``` ``` invalid.ts:11:20 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 9 │ let foo = <'bar'>'bar'; 10 │ let foo = <4>4; > 11 │ let foo = 'bar' as 'bar'; │ ^^^^^ 12 │ let foo = 5 as 5; 13 │ i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 9 9 │ let foo = <'bar'>'bar'; 10 10 │ let foo = <4>4; 11 │ - let·foo·=·'bar'·as·'bar'; 11 │ + let·foo·=·'bar'·as·const; 12 12 │ let foo = 5 as 5; 13 13 │ ``` ``` invalid.ts:12:16 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 10 │ let foo = <4>4; 11 │ let foo = 'bar' as 'bar'; > 12 │ let foo = 5 as 5; │ ^ 13 │ 14 │ class foo { i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 10 10 │ let foo = <4>4; 11 11 │ let foo = 'bar' as 'bar'; 12 │ - let·foo·=·5·as·5; 12 │ + let·foo·=·5·as·const; 13 13 │ 14 14 │ class foo { ``` ``` invalid.ts:15:8 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of type annotation. 14 │ class foo { > 15 │ bar: 'baz' = 'baz'; │ ^^^^^ 16 │ } 17 │ i as const doesn't require any update when the value is changed. i Safe fix: Replace with as const. 13 13 │ 14 14 │ class foo { 15 │ - ··bar:·'baz'·=·'baz'; 15 │ + ··bar·=·'baz'·as·const; 16 16 │ } 17 17 │ ``` ``` invalid.ts:19:8 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of type annotation. 18 │ class foo { > 19 │ bar: 2 = 2; │ ^ 20 │ } 21 │ i as const doesn't require any update when the value is changed. i Safe fix: Replace with as const. 17 17 │ 18 18 │ class foo { 19 │ - ··bar:·2·=·2; 19 │ + ··bar·=·2·as·const; 20 20 │ } 21 21 │ ``` ``` invalid.ts:23:10 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of angle bracket type assertion. 22 │ class foo { > 23 │ foo = <'bar'>'bar'; │ ^^^^^ 24 │ } 25 │ i The angle bracket assertion can occasionally be confused with JSX syntax, so using the as const is a preferable alternative. i Safe fix: Replace with as const. 21 21 │ 22 22 │ class foo { 23 │ - ··foo·=·<'bar'>'bar'; 23 │ + ··foo·=·'bar'·as·const; 24 24 │ } 25 25 │ ``` ``` invalid.ts:27:18 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 26 │ class foo { > 27 │ foo = 'bar' as 'bar'; │ ^^^^^ 28 │ } 29 │ i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 25 25 │ 26 26 │ class foo { 27 │ - ··foo·=·'bar'·as·'bar'; 27 │ + ··foo·=·'bar'·as·const; 28 28 │ } 29 29 │ ``` ``` invalid.ts:31:14 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 30 │ class foo { > 31 │ foo = 5 as 5; │ ^ 32 │ } 33 │ i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 29 29 │ 30 30 │ class foo { 31 │ - ··foo·=·5·as·5; 31 │ + ··foo·=·5·as·const; 32 32 │ } 33 33 │ ``` ``` invalid.ts:35:36 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 34 │ class foo { > 35 │ constructor(protected prop = 1 as 1) {} │ ^ 36 │ } 37 │ i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 33 33 │ 34 34 │ class foo { 35 │ - → constructor(protected·prop·=·1·as·1)·{} 35 │ + → constructor(protected·prop·=·1·as·const)·{} 36 36 │ } 37 37 │ ``` ``` invalid.ts:38:27 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 36 │ } 37 │ > 38 │ function foo(param = 1 as 1) {} │ ^ 39 │ 40 │ class foo { i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 36 36 │ } 37 37 │ 38 │ - function·foo(param·=·1·as·1)·{} 38 │ + function·foo(param·=·1·as·const)·{} 39 39 │ 40 40 │ class foo { ``` ``` invalid.ts:41:40 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 40 │ class foo { > 41 │ constructor(protected prop = 'bar' as 'bar') {} │ ^^^^^ 42 │ } 43 │ i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 39 39 │ 40 40 │ class foo { 41 │ - → constructor(protected·prop·=·'bar'·as·'bar')·{} 41 │ + → constructor(protected·prop·=·'bar'·as·const)·{} 42 42 │ } 43 43 │ ``` ``` invalid.ts:44:31 lint/style/useAsConstAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Use as const instead of as with a literal type. 42 │ } 43 │ > 44 │ function foo(param = 'bar' as 'bar') {} │ ^^^^^ 45 │ i as const doesn't require any update when the asserted value is changed. i Safe fix: Replace with as const. 42 42 │ } 43 43 │ 44 │ - function·foo(param·=·'bar'·as·'bar')·{} 44 │ + function·foo(param·=·'bar'·as·const)·{} 45 45 │ ```