Skip to content

JSDoc typedef module value type propagation fix #28256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,10 @@ namespace ts {
return unknownSymbol;
}
if (valueSymbol.flags & (SymbolFlags.Type | SymbolFlags.Namespace)) {
const declaration = <JSDocTypedefTag | JSDocCallbackTag | TypeAliasDeclaration>find(typeSymbol.declarations, d =>
isJSDocTypedefTag(d));
if (declaration) return typeSymbol;

return valueSymbol;
}
const result = createSymbol(valueSymbol.flags | typeSymbol.flags, valueSymbol.escapedName);
Expand Down Expand Up @@ -5192,6 +5196,10 @@ namespace ts {
return addOptionality(declaredType, isOptional);
}

if (isNamelessJSDocTypeDef(declaration)) {
return getDeclaredTypeOfTypeAlias(getSymbolOfNode(declaration));
}

if ((noImplicitAny || isInJSFile(declaration)) &&
declaration.kind === SyntaxKind.VariableDeclaration && !isBindingPattern(declaration.name) &&
!(getCombinedModifierFlags(declaration) & ModifierFlags.Export) && !(declaration.flags & NodeFlags.Ambient)) {
Expand Down Expand Up @@ -6352,8 +6360,10 @@ namespace ts {
return errorType;
}

const declaration = <JSDocTypedefTag | JSDocCallbackTag | TypeAliasDeclaration>find(symbol.declarations, d =>
let declaration = <JSDocTypedefTag | JSDocCallbackTag | TypeAliasDeclaration>find(symbol.declarations, d =>
isJSDocTypeAlias(d) || d.kind === SyntaxKind.TypeAliasDeclaration);
if (!declaration) declaration = findMap(symbol.declarations, getJSDocNamelessTypedefTag);

const typeNode = isJSDocTypeAlias(declaration) ? declaration.typeExpression : declaration.type;
// If typeNode is missing, we will error in checkJSDocTypedefTag.
let type = typeNode ? getTypeFromTypeNode(typeNode) : errorType;
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5355,6 +5355,11 @@ namespace ts {
return getJSDocTags(node).filter(doc => doc.kind === kind);
}

/** Gets the nameless JSDoc typedef tag for the node if present */
export function getJSDocNamelessTypedefTag(node: Node): JSDocTypedefTag | undefined {
return getFirstJSDocTag(node, (n): n is JSDocTypedefTag => isJSDocTypedefTag(n) && !n.name);
}

/**
* Gets the effective type parameters. If the node was parsed in a
* JavaScript file, gets the type parameters from the `@template` tag from JSDoc.
Expand Down Expand Up @@ -6124,6 +6129,10 @@ namespace ts {
export function isJSDocSignature(node: Node): node is JSDocSignature {
return node.kind === SyntaxKind.JSDocSignature;
}

export function isNamelessJSDocTypeDef(node: Node): boolean {
return !hasInitializer(node) && !!getJSDocNamelessTypedefTag(node);
}
}

// Node tests
Expand Down
3 changes: 3 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3367,6 +3367,8 @@ declare namespace ts {
function getJSDocTags(node: Node): ReadonlyArray<JSDocTag>;
/** Gets all JSDoc tags of a specified kind, or undefined if not present. */
function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray<JSDocTag>;
/** Gets the nameless JSDoc typedef tag for the node if present */
function getJSDocNamelessTypedefTag(node: Node): JSDocTypedefTag | undefined;
/**
* Gets the effective type parameters. If the node was parsed in a
* JavaScript file, gets the type parameters from the `@template` tag from JSDoc.
Expand Down Expand Up @@ -3546,6 +3548,7 @@ declare namespace ts {
function isJSDocTypeLiteral(node: Node): node is JSDocTypeLiteral;
function isJSDocCallbackTag(node: Node): node is JSDocCallbackTag;
function isJSDocSignature(node: Node): node is JSDocSignature;
function isNamelessJSDocTypeDef(node: Node): boolean;
}
declare namespace ts {
/**
Expand Down
3 changes: 3 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3367,6 +3367,8 @@ declare namespace ts {
function getJSDocTags(node: Node): ReadonlyArray<JSDocTag>;
/** Gets all JSDoc tags of a specified kind, or undefined if not present. */
function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray<JSDocTag>;
/** Gets the nameless JSDoc typedef tag for the node if present */
function getJSDocNamelessTypedefTag(node: Node): JSDocTypedefTag | undefined;
/**
* Gets the effective type parameters. If the node was parsed in a
* JavaScript file, gets the type parameters from the `@template` tag from JSDoc.
Expand Down Expand Up @@ -3546,6 +3548,7 @@ declare namespace ts {
function isJSDocTypeLiteral(node: Node): node is JSDocTypeLiteral;
function isJSDocCallbackTag(node: Node): node is JSDocCallbackTag;
function isJSDocSignature(node: Node): node is JSDocSignature;
function isNamelessJSDocTypeDef(node: Node): boolean;
}
declare namespace ts {
/**
Expand Down
97 changes: 97 additions & 0 deletions tests/baselines/reference/typedefCrossModule6.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
tests/cases/conformance/jsdoc/a.js(11,12): error TS2749: 'this_is_not_the_name' refers to a value, but is being used as a type here.
tests/cases/conformance/jsdoc/b.js(5,7): error TS2345: Argument of type '123' is not assignable to parameter of type 'string'.
tests/cases/conformance/jsdoc/b.js(15,7): error TS2345: Argument of type '123' is not assignable to parameter of type 'string'.
tests/cases/conformance/jsdoc/e.ts(6,26): error TS2322: Type '{ c: string; }' is not assignable to type '{ a: number; }'.
Object literal may only specify known properties, and 'c' does not exist in type '{ a: number; }'.


==== tests/cases/conformance/jsdoc/a.js (1 errors) ====
/**
* @typedef {number}
*/
export var numdef;

/**
* @typedef {string} strdef
*/
var this_is_not_the_name = true;

/** @type {this_is_not_the_name} */
~~~~~~~~~~~~~~~~~~~~
!!! error TS2749: 'this_is_not_the_name' refers to a value, but is being used as a type here.
let k; // should fail, this_is_not_the_name is not bound to the type def

/**
* @param {numdef} p1
* @param {strdef} p2
*/
function func1(p1, p2) {}

/**
* @param {strdef} p1
* @param {numdef} p2
*/
export function func2(p1, p2) {}


==== tests/cases/conformance/jsdoc/b.js (2 errors) ====
import { func2, strdef } from './a';
import * as mod from './a';

func2("123", 123);
func2(123, "123"); // should fail
~~~
!!! error TS2345: Argument of type '123' is not assignable to parameter of type 'string'.


/**
* @param {strdef} p1
* @param {mod.numdef} p2
*/
function func3(p1, p2) {}

func3("123", 123);
func3(123, {}); // should fail
~~~
!!! error TS2345: Argument of type '123' is not assignable to parameter of type 'string'.

/**
* @typedef {{ a: string, b: number, c: strdef, d: mod.numdef }}
*/
let objdef;

export {
objdef
};


==== tests/cases/conformance/jsdoc/c.js (0 errors) ====
import { objdef } from './b';

/**
* @param {objdef} p1
*/
function func4(p1) {}
func4({a: 'a', b: 0, c: 'c', d: 1});


==== tests/cases/conformance/jsdoc/d.js (0 errors) ====
/**
* @typedef {{ a: number }}
*/
let recordDef;
module.exports = { recordDef };


==== tests/cases/conformance/jsdoc/e.ts (1 errors) ====
import { numdef } from './a';
import { recordDef } from './d'; // commonjs exported nameless typedef

const ex: numdef = 123;
const ex2: recordDef = { a: ex };
const ex3: recordDef = { c: 'hi' }; // should fail
~~~~~~~
!!! error TS2322: Type '{ c: string; }' is not assignable to type '{ a: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type '{ a: number; }'.


135 changes: 135 additions & 0 deletions tests/baselines/reference/typedefCrossModule6.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
=== tests/cases/conformance/jsdoc/a.js ===
/**
* @typedef {number}
*/
export var numdef;
>numdef : Symbol(numdef, Decl(a.js, 3, 10), Decl(a.js, 1, 3))

/**
* @typedef {string} strdef
*/
var this_is_not_the_name = true;
>this_is_not_the_name : Symbol(this_is_not_the_name, Decl(a.js, 8, 3))

/** @type {this_is_not_the_name} */
let k; // should fail, this_is_not_the_name is not bound to the type def
>k : Symbol(k, Decl(a.js, 11, 3))

/**
* @param {numdef} p1
* @param {strdef} p2
*/
function func1(p1, p2) {}
>func1 : Symbol(func1, Decl(a.js, 11, 6))
>p1 : Symbol(p1, Decl(a.js, 17, 15))
>p2 : Symbol(p2, Decl(a.js, 17, 18))

/**
* @param {strdef} p1
* @param {numdef} p2
*/
export function func2(p1, p2) {}
>func2 : Symbol(func2, Decl(a.js, 17, 25))
>p1 : Symbol(p1, Decl(a.js, 23, 22))
>p2 : Symbol(p2, Decl(a.js, 23, 25))


=== tests/cases/conformance/jsdoc/b.js ===
import { func2, strdef } from './a';
>func2 : Symbol(func2, Decl(b.js, 0, 8))
>strdef : Symbol(strdef, Decl(b.js, 0, 15))

import * as mod from './a';
>mod : Symbol(mod, Decl(b.js, 1, 6))

func2("123", 123);
>func2 : Symbol(func2, Decl(b.js, 0, 8))

func2(123, "123"); // should fail
>func2 : Symbol(func2, Decl(b.js, 0, 8))


/**
* @param {strdef} p1
* @param {mod.numdef} p2
*/
function func3(p1, p2) {}
>func3 : Symbol(func3, Decl(b.js, 4, 18))
>p1 : Symbol(p1, Decl(b.js, 11, 15))
>p2 : Symbol(p2, Decl(b.js, 11, 18))

func3("123", 123);
>func3 : Symbol(func3, Decl(b.js, 4, 18))

func3(123, {}); // should fail
>func3 : Symbol(func3, Decl(b.js, 4, 18))

/**
* @typedef {{ a: string, b: number, c: strdef, d: mod.numdef }}
*/
let objdef;
>objdef : Symbol(objdef, Decl(b.js, 19, 3), Decl(b.js, 17, 3))

export {
objdef
>objdef : Symbol(objdef, Decl(b.js, 21, 8), Decl(b.js, 17, 3))

};


=== tests/cases/conformance/jsdoc/c.js ===
import { objdef } from './b';
>objdef : Symbol(objdef, Decl(c.js, 0, 8))

/**
* @param {objdef} p1
*/
function func4(p1) {}
>func4 : Symbol(func4, Decl(c.js, 0, 29))
>p1 : Symbol(p1, Decl(c.js, 5, 15))

func4({a: 'a', b: 0, c: 'c', d: 1});
>func4 : Symbol(func4, Decl(c.js, 0, 29))
>a : Symbol(a, Decl(c.js, 6, 7))
>b : Symbol(b, Decl(c.js, 6, 14))
>c : Symbol(c, Decl(c.js, 6, 20))
>d : Symbol(d, Decl(c.js, 6, 28))


=== tests/cases/conformance/jsdoc/d.js ===
/**
* @typedef {{ a: number }}
*/
let recordDef;
>recordDef : Symbol(recordDef, Decl(d.js, 3, 3), Decl(d.js, 1, 3))

module.exports = { recordDef };
>module.exports : Symbol("tests/cases/conformance/jsdoc/d", Decl(d.js, 0, 0))
>module : Symbol(export=, Decl(d.js, 3, 14))
>exports : Symbol(export=, Decl(d.js, 3, 14))
>recordDef : Symbol(recordDef, Decl(d.js, 4, 18))


=== tests/cases/conformance/jsdoc/e.ts ===
import { numdef } from './a';
>numdef : Symbol(numdef, Decl(e.ts, 0, 8))

import { recordDef } from './d'; // commonjs exported nameless typedef
>recordDef : Symbol(recordDef, Decl(e.ts, 1, 8))

const ex: numdef = 123;
>ex : Symbol(ex, Decl(e.ts, 3, 5))
>numdef : Symbol(numdef, Decl(e.ts, 0, 8))

const ex2: recordDef = { a: ex };
>ex2 : Symbol(ex2, Decl(e.ts, 4, 5))
>recordDef : Symbol(recordDef, Decl(e.ts, 1, 8))
>a : Symbol(a, Decl(e.ts, 4, 24))
>ex : Symbol(ex, Decl(e.ts, 3, 5))

const ex3: recordDef = { c: 'hi' }; // should fail
>ex3 : Symbol(ex3, Decl(e.ts, 5, 5))
>recordDef : Symbol(recordDef, Decl(e.ts, 1, 8))
>c : Symbol(c, Decl(e.ts, 5, 24))


Loading