GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(`no-unnecessary-type-assertion`): widen literal types on let and … · gajus/eslint-plugin-jsdoc@6f90f25

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs/rules
    • no-unnecessary-type-assertion.md
  • src/rules
    • noUnnecessaryTypeAssertion.js
  • test/rules/assertions
    • noUnnecessaryTypeAssertion.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: docs/rules/no-unnecessary-type-assertion.md
+49Lines changed: 49 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,30 @@ export const first = /** @type {string} */ (text);
413413
export const second = /** @type {string} */ (text);
414414
// "jsdoc/no-unnecessary-type-assertion": ["error"|"warn", {"enableFixer":false}]
415415
// Message: The @type tag declaring "string" is redundant as TypeScript infers it automatically.
416+
417+
/**
418+
* @type {'a' | 'b'}
419+
*/
420+
const mode = 'a';
421+
// Message: The @type tag declaring "'a' | 'b'" is redundant as TypeScript infers it automatically.
422+
423+
/**
424+
* @type {string}
425+
*/
426+
let mode = 'a';
427+
// Message: The @type tag declaring "string" is redundant as TypeScript infers it automatically.
428+
429+
/**
430+
* @type {string}
431+
*/
432+
var mode = 'a';
433+
// Message: The @type tag declaring "string" is redundant as TypeScript infers it automatically.
434+
435+
/**
436+
* @type {{mode: string}}
437+
*/
438+
const {mode} = {mode: 'a'};
439+
// Message: The @type tag declaring "{mode: string}" is redundant as TypeScript infers it automatically.
416440
````
417441

418442

@@ -658,5 +682,30 @@ const keyRange = {
658682
...base,
659683
run: (arg) => arg,
660684
};
685+
686+
/**
687+
* @type {'a' | 'b'}
688+
*/
689+
let mode = 'a';
690+
691+
/**
692+
* @type {'a' | 'b'}
693+
*/
694+
var mode = 'a';
695+
696+
/**
697+
* @type {{mode: 'a' | 'b'}}
698+
*/
699+
let {mode} = {mode: 'a'};
700+
701+
/**
702+
* @type {{mode: 'a' | 'b'}}
703+
*/
704+
var {mode} = {mode: 'a'};
705+
706+
/**
707+
* @type {'a' | 'b'}
708+
*/
709+
let mode = 'a', other = 'c';
661710
````
662711

Collapse file

‎src/rules/noUnnecessaryTypeAssertion.js‎

Copy file name to clipboardExpand all lines: src/rules/noUnnecessaryTypeAssertion.js
+28-5Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,23 @@ export default iterateJsdoc(({
482482
'VariableDeclarator',
483483
]);
484484

485+
/**
486+
* The type TypeScript infers for an initializer. A mutable binding (`let` or
487+
* `var`) widens a literal (`'a'` becomes `string`, `5` becomes `number`), so
488+
* a `@type` that only matches the unwidened literal is still narrowing. A
489+
* `const` binding keeps the literal type.
490+
* @param {any} initializerTsNode `ts.Node`
491+
* @param {boolean} mutableBinding `true` when the declaration is not `const`
492+
* @returns {any} `ts.Type`
493+
*/
494+
const inferredInitializerType = (initializerTsNode, mutableBinding) => {
495+
const inferredType = checker.getTypeAtLocation(initializerTsNode);
496+
497+
return mutableBinding ?
498+
checker.getBaseTypeOfLiteralType(inferredType) :
499+
inferredType;
500+
};
501+
485502
// 3. `/** @type {T} */ const x = 5;`
486503
if (node?.type === 'VariableDeclaration') {
487504
// A leading `@type` tag only influences the first declarator (TypeScript
@@ -513,7 +530,13 @@ export default iterateJsdoc(({
513530
return;
514531
}
515532

516-
const declInferredType = checker.getTypeAtLocation(declInitTsNode);
533+
// Widen `let`/`var` the same way as an inline cast. Otherwise
534+
// `/** @type {'a' | 'b'} */ let mode = 'a'` looks redundant, and removing
535+
// the tag widens `mode` to `string`.
536+
const declInferredType = inferredInitializerType(
537+
declInitTsNode,
538+
node.kind !== 'const',
539+
);
517540
const declAssertedType = checker.getTypeFromTypeNode(jsdocTypeNode);
518541

519542
if (isTupleConversionIdiom(declAssertedType, decl.init)) {
@@ -600,10 +623,10 @@ export default iterateJsdoc(({
600623
// For a mutable binding (`let`/`var`) TypeScript widens the initializer, so a
601624
// narrowing cast such as `/** @type {5} */` is doing real work; widen the
602625
// uncast type to match before judging redundancy.
603-
const mutableBinding = Boolean(declaration) && declaration.kind !== 'const';
604-
const castInferredType = mutableBinding ?
605-
checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(exprTsNode)) :
606-
checker.getTypeAtLocation(exprTsNode);
626+
const castInferredType = inferredInitializerType(
627+
exprTsNode,
628+
Boolean(declaration) && declaration.kind !== 'const',
629+
);
607630
const castAssertedType = checker.getTypeFromTypeNode(typeTag.typeExpression.type);
608631

609632
if (isTupleConversionIdiom(castAssertedType, node)) {
Collapse file

‎test/rules/assertions/noUnnecessaryTypeAssertion.js‎

Copy file name to clipboardExpand all lines: test/rules/assertions/noUnnecessaryTypeAssertion.js
+126Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,82 @@ export default /** @type {import('../index.js').TestCases} */ ({
873873
],
874874
output: null,
875875
},
876+
{
877+
code: `
878+
/**
879+
* @type {'a' | 'b'}
880+
*/
881+
const mode = 'a';
882+
`,
883+
errors: [
884+
{
885+
line: 3,
886+
message: 'The @type tag declaring "\'a\' | \'b\'" is redundant as TypeScript infers it automatically.',
887+
},
888+
],
889+
filename: 'dummy.js',
890+
languageOptions,
891+
output: `
892+
const mode = 'a';
893+
`,
894+
},
895+
{
896+
code: `
897+
/**
898+
* @type {string}
899+
*/
900+
let mode = 'a';
901+
`,
902+
errors: [
903+
{
904+
line: 3,
905+
message: 'The @type tag declaring "string" is redundant as TypeScript infers it automatically.',
906+
},
907+
],
908+
filename: 'dummy.js',
909+
languageOptions,
910+
output: `
911+
let mode = 'a';
912+
`,
913+
},
914+
{
915+
code: `
916+
/**
917+
* @type {string}
918+
*/
919+
var mode = 'a';
920+
`,
921+
errors: [
922+
{
923+
line: 3,
924+
message: 'The @type tag declaring "string" is redundant as TypeScript infers it automatically.',
925+
},
926+
],
927+
filename: 'dummy.js',
928+
languageOptions,
929+
output: `
930+
var mode = 'a';
931+
`,
932+
},
933+
{
934+
code: `
935+
/**
936+
* @type {{mode: string}}
937+
*/
938+
const {mode} = {mode: 'a'};
939+
`,
940+
errors: [
941+
{
942+
line: 3,
943+
message: 'The @type tag declaring "{mode: string}" is redundant as TypeScript infers it automatically.',
944+
},
945+
],
946+
filename: 'dummy.js',
947+
languageOptions,
948+
output: `
949+
const {mode} = {mode: 'a'};
950+
`,
951+
},
876952
],
877953
valid: [
878954
{
@@ -1360,5 +1436,55 @@ export default /** @type {import('../index.js').TestCases} */ ({
13601436
filename: 'dummy.js',
13611437
languageOptions,
13621438
},
1439+
{
1440+
code: `
1441+
/**
1442+
* @type {'a' | 'b'}
1443+
*/
1444+
let mode = 'a';
1445+
`,
1446+
filename: 'dummy.js',
1447+
languageOptions,
1448+
},
1449+
{
1450+
code: `
1451+
/**
1452+
* @type {'a' | 'b'}
1453+
*/
1454+
var mode = 'a';
1455+
`,
1456+
filename: 'dummy.js',
1457+
languageOptions,
1458+
},
1459+
{
1460+
code: `
1461+
/**
1462+
* @type {{mode: 'a' | 'b'}}
1463+
*/
1464+
let {mode} = {mode: 'a'};
1465+
`,
1466+
filename: 'dummy.js',
1467+
languageOptions,
1468+
},
1469+
{
1470+
code: `
1471+
/**
1472+
* @type {{mode: 'a' | 'b'}}
1473+
*/
1474+
var {mode} = {mode: 'a'};
1475+
`,
1476+
filename: 'dummy.js',
1477+
languageOptions,
1478+
},
1479+
{
1480+
code: `
1481+
/**
1482+
* @type {'a' | 'b'}
1483+
*/
1484+
let mode = 'a', other = 'c';
1485+
`,
1486+
filename: 'dummy.js',
1487+
languageOptions,
1488+
},
13631489
],
13641490
});

0 commit comments

Comments
0 (0)
You are visitor 01402622 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.