Skip to content

In JS, @type tag doesn't apply generic type to initialiser #42060

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
awerlogus opened this issue Dec 21, 2020 · 3 comments · Fixed by #43688
Closed

In JS, @type tag doesn't apply generic type to initialiser #42060

awerlogus opened this issue Dec 21, 2020 · 3 comments · Fixed by #43688
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@awerlogus
Copy link

awerlogus commented Dec 21, 2020

TypeScript Version: 4.2.0-dev.20201219

Search Terms: template type lost js

Code

/** @template T @typedef {<T1 extends T>(data: T1) => T1} Test */

/** @type {Test<number>} */
const test = data => data

Expected behavior:
data has type T1 extends number

Actual behavior:
data has any type
изображение

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Dec 23, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.3.0 milestone Dec 23, 2020
@sandersn
Copy link
Member

sandersn commented Apr 8, 2021

Note that if you call the function, the type, and the errors are correct:

test(1) // T1=1
test('hi') // error, and T1=number (the default)

It's just not applying the type to the initialiser. Might be a bug in contextual typing?

@sandersn sandersn added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Apr 8, 2021
@sandersn sandersn changed the title Template type lost in js In JS, @type tag doesn't apply generic type to initialiser Apr 8, 2021
@sandersn
Copy link
Member

This behaviour is the intended effect of #26236, which says it's trying to avoid circular return type errors. But the bug it fixes only mentions typescript.

I'm going to turn off the erasure to any and see what tests fail.

@sandersn
Copy link
Member

No tests fail; #26236 never tested the type tag erasure change and it doesn't look correct to me. I'll remove it and send a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants