@typedef {Function}
generates a type that can be read, but can't be applied
#50274
Labels
Domain: JSDoc
Relates to JSDoc parsing and type generation
Experience Enhancement
Noncontroversial enhancements
Suggestion
An idea for TypeScript
Milestone
🔎 Search Terms
callback typedef function alias semantic jsdoc tsc tsserver
🕗 Version & Regression Information
Always, up to Version 4.7.4
Summary
You should be able to create a typedef of a
Function
and use that type for any applicable function.For example:
However, this fails:
Complete Example
⏯ Playground Link
https://www.typescriptlang.org/play?filetype=js#code/EQVwzgpgBGAuBOBLAxrYBuAUJg9AKj0yjygAFYBPABwgBMIAzKAbwHkAjAKwlQF8oAChHhgA9gDsiJUlXiia8SiwDKCROIDm-cQEMAthCllZ84UuZCREgOLwIEWPw12HUnNnyFiZSjXpNmADEQcVRECX5LMXFbe1gjGR14fRYoiKhRWAALYQS7WBB4cTAVNU1eN2wAGwdBYWioAF4WXixcAgSqJJS2Lh5HKCpO7r1SpHLBgDpdAzyHQuLU+ojKtPFJ5DsdWGhmhhCwiSgACioAShYiKGua2EHl8SaWtuvrhWjp-V2pmYgX188Pmo0DWsVqeHcr3uVnWzjiT32oVg4UecIcx0yOXgF2YVyh13yCygAAMABIQKpVURQAAkzExwk+Bl4ABooHoKFBflBECU6e8JEyILwAITErBQ1rYKGEorQ6JYKWYPSiWggGqTCAADyookUJWaa3QQA
💻 Code
🙁 Actual behavior
In the example you can see that the checker doesn't apply the type
PersonGreet
to the function being assigned toperson.greet
.The type declared with
/** @typedef {Function} */
is parsed, but it can't be used to to type a compliant function directly (although it is somewhat usable as an export that other modules can understand the type in some contexts).🙂 Expected behavior
A type generated with
@typedef {Function}
should be able to be applied to functions.Workaround
There's no documentation for this, but it just so happens that if you use the
@callback
alias some extra machinery kicks in and you can type functions as you would have expected.The problem is that
@callback
is usually incorrect and confusing:It's also possible to use TypeScript's proprietary arrow notation, but this generates other errors:
The text was updated successfully, but these errors were encountered: