-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Typing a non-arrow function makes for some partially working result #56636
Comments
Duplicate of #22063. This is a missing feature, not a bug. |
Interesting, thanks for the heads-up. What tricked me is the fact that it seems to work using the |
Functions can be expressions as well, so TypeScript probably interprets it as an expression that is being type asserted, not a top-level function declaration. const test = <any>function foo() {} |
Good point. |
I believe that's what's happening, as you can see in the JS output it uses parens to maintain the function expression (and not function definition) syntax: (function doSomething(foo) { ... }) Given that JS output, the runtime JS behavior is understandable |
So basically everything works as expected and it is perfectly possible to type a function - but it is considered as an expression that needs to be assigned to a variable to be referenced later. Did I understand things properly? |
Yes, that's always been allowed (and will get a good contextual type and everything): var x: (value: string) => string = function bah(a) {
return a + 4;
} But it's a different thing than a function definition and behaves differently at runtime. |
Many thanks. I was totally unaware of that. |
π Search Terms
type function
type non arrow function
π Version & Regression Information
I didn't test outside of TS 5.2.2 but I assume it was not working before either.
β― Playground Link
No response
π» Code
π Actual behavior
Here, both
foo
and the return type ofdoSomething
are perfectly inferred - returning astring
would fail the compilation, just like usingfoo
as anything else than a number.But...the next instruction
doSomething(5);
throws a "TS2552: Cannot find namedoSomething
." which obviously makes the code useless.π Expected behavior
I'm not sure. Ideally I would expect that it works - i.e. that using
doSomething
remains possible - but I'm not sure it was ever intended to type non-arrow function at all.What is strange though is that it seems to work just fine, until we try to actually use the function.
Additional information about the issue
This is related to Rollup plugin development where plugin hooks are non-arrow function. It would be amazing if we could type them individually to organize the code better instead of having to put everything under an object definition typed as a
Plugin
, like this:With this syntax, obviously, the
transform
method type is correctly inferred but it would be cool if we were allowed to write it this way for better reusability:See rollup/rollup#5277
The text was updated successfully, but these errors were encountered: