Skip to content

jsdoc @template doesn't work correctly if the comment is before an object property name #17339

Closed
@ghost

Description

TypeScript Version: 2.4.1 and 2.5.0-dev.20170719

Code

// @ts-check

var obj = {
	
	/**
	 * hover: "(property) no_workaround: <T>(a: any[]) => any"  
	 * T in "param" and "returns" say "Cannot find name 'T'."
	 * @template T
	 * @param {T[]} a
	 * @returns {T}
	 */
	no_workaround: function (a) {
		return a[0];
	},
	
	yes_workaround:
	/**
	 * this works correctly: "(property) yes_workaround: <T>(a: T[]) => T"
	 * @template T
	 * @param {T[]} a
	 * @returns {T}
	 */
	function (a) {
		return a[0];
	}
};

// correct return type - "Property 'nonexistent' does not exist on type 'number'."
obj.yes_workaround([1]).nonexistent();

// return type is any so there's no type checking
obj.no_workaround([1]).nonexistent();

Expected behavior:

the type of no_workaround should be the same as yes_workaround (<T>(a: T[]) => T)

Actual behavior:

it's something else: <T>(a: any[]) => any

the type parameter from @template is added to the function's signature but it seems like it's not in scope for the other jsdoc tags (so they show an error and use any), unless the comment block appears right before the function expression like in yes_workaround

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: JSDocRelates to JSDoc parsing and type generationFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions