-
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
Surprising behavior with typeof
in signatures
#22825
Comments
It gets even weirder if you reference variables in the function body: declare let foo: number;
// bar is of type string
// return type is number
function fn(bar: typeof foo): typeof foo {
var foo = '';
return bar; // compile error: string is not assignable to number
} But if you change declare let foo: number;
// bar is of type string
// return type is string
function fn(bar: typeof foo): typeof foo {
let foo = '';
return bar; // no compile error
} |
In |
I'll still mark this as a bug because I would expect the second example to have the same behavior regardless of whether you write |
@andy-ms
If you copy and paste the whole snippet including the Also that's what I meant with this note in the OP:
|
Ah, OK. So the problem is that we do name lookup differently in the return type vs in the parameter. |
Exactly. At least it should be consistent and not change if a parameter is destructured. |
TypeScript Version: 2.9.0-dev.20180323
Search Terms:
Code
Expected behavior:
typeof foo
should reference the same declaration regardless of where it's used in the signature.Actual behavior:
typeof foo
in return type refers to the outer variable iffoo
comes from a destructured parameter.typeof foo
inside the type annotation of another parameter refers to the parameter as expected.This becomes even more visible if you remove the outer
declare let foo: number;
which will result in a compile error:Cannot find name 'foo'.
Playground Link: http://www.typescriptlang.org/play/#src=declare%20let%20foo%3A%20number%3B%0A%0Adeclare%20function%20test(foo%3A%20string%2C%20bar%3A%20typeof%20foo)%3A%20typeof%20foo%3B%0Adeclare%20function%20test(%7Bfoo%7D%3A%20%7Bfoo%3A%20string%7D%2C%20bar%3A%20typeof%20foo)%3A%20typeof%20foo%3B
Related Issues:
#22769 possibly related issue where the parameter initializer refers to the wrong variable.
The text was updated successfully, but these errors were encountered: