Skip to content

Commit

Permalink
fix(no-undefined-types): add TypeScript utility types (globally all…
Browse files Browse the repository at this point in the history
…owed types); fixes #839
  • Loading branch information
brettz9 committed Mar 29, 2022
1 parent 63a96ee commit bf9f994
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Expand Up @@ -9889,6 +9889,18 @@ function foo () {
}
// Settings: {"jsdoc":{"mode":"jsdoc"}}
// Message: The type 'visibility' is undefined.

/**
* @typedef Todo
* @property description
* @property otherStuff
*/
/**
* @type {Omit<Todo, "description">}
*/
const a = new Todo();
// Settings: {"jsdoc":{"mode":"jsdoc"}}
// Message: The type 'Omit' is undefined.
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -10302,6 +10314,17 @@ export class Foo {
* @type {const}
*/
const a = 'string';

/**
* @typedef Todo
* @property description
* @property otherStuff
*/
/**
* @type {Omit<Todo, "description">}
*/
const a = new Todo();
// Settings: {"jsdoc":{"mode":"typescript"}}
````


Expand Down
33 changes: 32 additions & 1 deletion src/rules/noUndefinedTypes.js
Expand Up @@ -19,6 +19,30 @@ const extraTypes = [
'Array', 'Object', 'RegExp', 'Date', 'Function',
];

// https://www.typescriptlang.org/docs/handbook/utility-types.html
const typescriptGlobals = [
'Partial',
'Required',
'Readonly',
'Record',
'Pick',
'Omit',
'Exclude',
'Extract',
'NonNullable',
'Parameters',
'ConstructorParameters',
'ReturnType',
'InstanceType',
'ThisParameterType',
'OmitThisParameter',
'ThisType',
'Uppercase',
'Lowercase',
'Capitalize',
'Uncapitalize',
];

const stripPseudoTypes = (str) => {
return str && str.replace(/(?:\.|<>|\.<>|\[\])$/u, '');
};
Expand Down Expand Up @@ -151,7 +175,14 @@ export default iterateJsdoc(({
.concat(typedefDeclarations)
.concat(definedTypes)
.concat(definedPreferredTypes)
.concat(settings.mode === 'jsdoc' ? [] : closureGenericTypes));
.concat(
settings.mode === 'jsdoc' ?
[] :
[
...settings.mode === 'typescript' ? typescriptGlobals : [],
...closureGenericTypes,
],
));

const jsdocTagsWithPossibleType = utils.filterTags(({
tag,
Expand Down
42 changes: 42 additions & 0 deletions test/rules/assertions/noUndefinedTypes.js
Expand Up @@ -460,6 +460,30 @@ export default {
},
},
},
{
code: `
/**
* @typedef Todo
* @property description
* @property otherStuff
*/
/**
* @type {Omit<Todo, "description">}
*/
const a = new Todo();
`,
errors: [
{
line: 8,
message: 'The type \'Omit\' is undefined.',
},
],
settings: {
jsdoc: {
mode: 'jsdoc',
},
},
},
],
valid: [
{
Expand Down Expand Up @@ -1216,5 +1240,23 @@ export default {
const a = 'string';
`,
},
{
code: `
/**
* @typedef Todo
* @property description
* @property otherStuff
*/
/**
* @type {Omit<Todo, "description">}
*/
const a = new Todo();
`,
settings: {
jsdoc: {
mode: 'typescript',
},
},
},
],
};

0 comments on commit bf9f994

Please sign in to comment.