Skip to content

Commit 6a4c8fe

Browse files
committed
fix(no-unnecessary-type-assertion): no exit early when typescript is found; fixes #1816
1 parent 2ba7ffd commit 6a4c8fe

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎docs/rules/no-unnecessary-type-assertion.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,13 @@ export function w8b(/** @type {string} */ s) {
406406
const wide = 'hello';
407407
// "jsdoc/no-unnecessary-type-assertion": ["error"|"warn", {"enableFixer":false,"fixTypeChangingCasts":false}]
408408
// Message: The @type tag declaring "string | number" is redundant as TypeScript infers it automatically.
409+
410+
const text = String(1);
411+
412+
export const first = /** @type {string} */ (text);
413+
export const second = /** @type {string} */ (text);
414+
// "jsdoc/no-unnecessary-type-assertion": ["error"|"warn", {"enableFixer":false}]
415+
// Message: The @type tag declaring "string" is redundant as TypeScript infers it automatically.
409416
````
410417

411418

‎src/rules/noUnnecessaryTypeAssertion.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,10 @@ export default iterateJsdoc(({
7777

7878
// eslint-disable-next-line no-console -- Warning user
7979
console.warn('⚠️ typescript is not installed. `jsdoc/no-unnecessary-type-assertion` will not work. To disable this warning, you must disable the rule.');
80+
return;
8081
} finally {
8182
tsAttempted = true;
8283
}
83-
84-
return;
8584
}
8685

8786
const types = utils.getTags('type');

‎test/rules/assertions/noUnnecessaryTypeAssertion.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,32 @@ export default /** @type {import('../index.js').TestCases} */ ({
847847
],
848848
output: null,
849849
},
850+
{
851+
code: `
852+
const text = String(1);
853+
854+
export const first = /** @type {string} */ (text);
855+
export const second = /** @type {string} */ (text);
856+
`,
857+
errors: [
858+
{
859+
line: 4,
860+
message: 'The @type tag declaring "string" is redundant as TypeScript infers it automatically.',
861+
},
862+
{
863+
line: 5,
864+
message: 'The @type tag declaring "string" is redundant as TypeScript infers it automatically.',
865+
},
866+
],
867+
filename: 'dummy.js',
868+
languageOptions,
869+
options: [
870+
{
871+
enableFixer: false,
872+
},
873+
],
874+
output: null,
875+
},
850876
],
851877
valid: [
852878
{

0 commit comments

Comments
 (0)