-
Notifications
You must be signed in to change notification settings - Fork 658
Fixed contextually typed expando members #704
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
Conversation
internal/checker/checker.go
Outdated
if links.resolvedType == nil { | ||
expando := c.getSymbolOfExpando(symbol.ValueDeclaration) | ||
if expando != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this ports Strada's mergeJSSymbols
- but since JS is not really involved here right now, I decided to just inline its relevant parts here (at least for the time being)
const c: Combo = () => 1 | ||
>c : Combo | ||
->() => 1 : { (): number; p: {}; } | ||
+>() => 1 : { (): number; p: { [s: string]: number; }; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not investigated why this is different but I genuinely feel like this is an improvement. It's not even a user-facing - or I don't know how to position my cursor in this example to get such quick info.
If we think about types alone then we can test how this behaves if we start assigning properties to p
(TS playground):
interface Combo {
(): number;
p?: { [s: string]: number };
}
const c: Combo = () => 1;
c.p = {};
c.p.testOk = 1; // ok
c.p.testError = "foo"; // error
So it's not really correct, in my eyes, to say that p
has {}
type. At any stage of the process. Either we know here about everything about expando members or we shouldn't know anything (if this would print a flow type or smth).
If I get the .types
print for the above in Strada I end up with this:
interface Combo {
(): number;
p?: { [s: string]: number };
>p : { [s: string]: number; } | undefined
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>s : string
> : ^^^^^^
}
const c: Combo = () => 1;
>c : Combo
> : ^^^^^
>() => 1 : { (): number; p: {}; }
> : ^^^^^^^^^^^^^^^^^^^^^^
>1 : 1
> : ^
c.p = {};
>c.p = {} : {}
> : ^^
>c.p : { [s: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>c : Combo
> : ^^^^^
>p : { [s: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{} : {}
> : ^^
c.p.testOk = 1;
>c.p.testOk = 1 : 1
> : ^
>c.p.testOk : number
> : ^^^^^^
>c.p : { [s: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>c : Combo
> : ^^^^^
>p : { [s: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>testOk : number
> : ^^^^^^
>1 : 1
> : ^
c.p.testError = "foo";
>c.p.testError = "foo" : "foo"
> : ^^^^^
>c.p.testError : number
> : ^^^^^^
>c.p : { [s: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>c : Combo
> : ^^^^^
>p : { [s: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>testError : number
> : ^^^^^^
>"foo" : "foo"
> : ^^^^^
I don't think we need any of this logic. I already have a fix for the errors in the In case you're curious, the fix we need is in the symbol := c.getExportSymbolOfValueSymbolIfExported(c.getResolvedSymbol(node.Expression())) |
Hm, I took another look at them right now and both look pretty OK to me.
Cool. That makes sense! I was looking at that function but I followed the Strada's compatibility~ (in terms of how some of those involved nodes/structures were connected) instead 😅 |
Some of the code in Strada related to expando properties is overly complicated and at times incorrect, in particular when it comes to our support for inference in .js files. In general, the implementation of our support for .js and JSDoc is going to differ from Strada. For example, see #610. Conceptually, in a construct like the following const fn: SomeType = () => { ... }
fn.prop = someExpr the type of the expression |
No description provided.