-
-
Notifications
You must be signed in to change notification settings - Fork 484
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
Incorrect reference resolve in function parameter's type #3799
Comments
I'm tempted to add |
This is tricky. As the example above demonstrates, there are two parallel scope chains - variables and types. Other examples which make this even clearer: // 2 bindings with same name in same scope
type Foo = number;
const Foo: Foo = 123;
function f(foo: Foo) { // `Foo` here refers to `type Foo`
return Foo; // `Foo` here refers to `const Foo`
}
let Bar = 456;
function f2<Bar>(foo: Bar) { // `Bar` here refers to `<Bar>`
return Bar; // `Bar` here refers to `let Bar`
} To make it worse, these 2 scope chains are not entirely independent. There are places where a binding can be "dual use" - can be used as either a variable or a type: import X from 'blah';
function f2(x: X) { return X; }
class Y {}
function f(y: Y) { return Y; } Are there any other "dual use" cases apart from I think we have 2 choices:
Option 1 is ideal from a completeness perspective, and could be useful for tools which want to do any form of type-checking/type analysis e.g. linter. But it'd require major changes to design of scope tree - need to support 2 bindings with same name in a scope. Option 2 solves the problem by going the other way - completely opting out of resolving type identifier references. |
If we go with option 2, and don't resolve type references, I'm not sure we can accurately determine which imports to strip out in TS->JS transform. e.g.: import Foo from 'foo';
function f(foo: Foo) {}
import Bar from 'bar';
function f2<Bar>(bar: Bar) {} tsc strips out both |
Meaning-based symbol resolution is a requirement if we want to include symbols in the symbol table. This means that code such as type T = number
const x = T also does not work right now. I am strongly against removing types from our symbol table, since it will hinder our ability to effectively lint and process typescript code. My end goal is to support type-aware linting and minification, and removing type symbols will be a step backwards in this regard. Some useful references on meaning-based symbol resolution are below:
I agree that this PR is not complete until we support type read references; I see a lack of this resolution as a bug. I'm not certain if this PR should contain a fix for it or if we should make another, separate PR. Unfortunately I cannot make stacked PRs since I'm not part of the Graphite organisation. |
The PR Don is referring to in his comment above is #3863. |
React has side effect when import: typescript-eslint/typescript-eslint#2455 (comment) The below cases also need a value reference, even it doesn't have: import React from 'react';
export const ComponentFoo: React.FC = () => {
return <div>Foo Foo</div>;
}; |
…rts` (#3895) This PR only contains the part about report error, adding the fixer part will make the whole PR difficult to review at one time. There are also some commented cases. One kind of them is `decorator`, as it blocked by #3645, another kind of them is type reference, need to solve #3799 first. I added TODO flags for them.
We treat function parameter
foo
's type refers to itself.The text was updated successfully, but these errors were encountered: