Skip to content

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,9 +989,13 @@ func (b *Binder) bindFunctionPropertyAssignment(node *ast.Node) {
case ast.IsFunctionDeclaration(symbol.ValueDeclaration):
funcSymbol = symbol
case ast.IsVariableDeclaration(symbol.ValueDeclaration) && symbol.ValueDeclaration.Parent.Flags&ast.NodeFlagsConst != 0:
initializer := symbol.ValueDeclaration.Initializer()
if initializer != nil && ast.IsFunctionExpressionOrArrowFunction(initializer) {
funcSymbol = initializer.Symbol()
if symbol.ValueDeclaration.Type() != nil {
funcSymbol = symbol
} else {
initializer := symbol.ValueDeclaration.Initializer()
if initializer != nil && ast.IsFunctionExpressionOrArrowFunction(initializer) {
funcSymbol = initializer.Parent.Symbol()
}
}
}
if funcSymbol != nil {
Expand Down
75 changes: 74 additions & 1 deletion internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13516,6 +13516,20 @@ func (c *Checker) getParentOfSymbol(symbol *ast.Symbol) *ast.Symbol {
return nil
}

func (c *Checker) getFunctionExpressionParentSymbolOrSymbol(symbol *ast.Symbol) *ast.Symbol {
declaration := symbol.ValueDeclaration
if declaration == nil {
return symbol
}
if declaration.Kind == ast.KindArrowFunction || declaration.Kind == ast.KindFunctionExpression {
parentSymbol := c.getSymbolOfNode(declaration.Parent)
if parentSymbol != nil {
return parentSymbol
}
}
return symbol
}

func (c *Checker) recordMergedSymbol(target *ast.Symbol, source *ast.Symbol) {
c.mergedSymbols[source] = target
}
Expand Down Expand Up @@ -14633,7 +14647,7 @@ func (c *Checker) getResolvedMembersOrExportsOfSymbol(symbol *ast.Symbol, resolu
}
}
if isStatic {
for member := range symbol.AssignmentDeclarationMembers.Keys() {
for member := range c.getFunctionExpressionParentSymbolOrSymbol(symbol).AssignmentDeclarationMembers.Keys() {
if c.hasLateBindableName(member) {
if lateSymbols == nil {
lateSymbols = make(ast.SymbolTable)
Expand Down Expand Up @@ -15506,8 +15520,31 @@ func (c *Checker) widenTypeInferredFromInitializer(declaration *ast.Node, t *Typ

func (c *Checker) getTypeOfFuncClassEnumModule(symbol *ast.Symbol) *Type {
links := c.valueSymbolLinks.Get(symbol)
originalLinks := links
if links.resolvedType == nil {
expando := c.getSymbolOfExpando(symbol.ValueDeclaration)
if expando != nil {
inferred := core.IfElse(symbol.Flags&ast.SymbolFlagsTransient != 0, symbol, nil)
if inferred == nil {
inferred = c.cloneSymbol(symbol)
}
if len(expando.Exports) != 0 {
if inferred.Exports == nil {
inferred.Exports = make(ast.SymbolTable)
}
c.mergeSymbolTable(inferred.Exports, expando.Exports, false, nil)
}
if len(expando.Members) != 0 {
if inferred.Members == nil {
inferred.Members = make(ast.SymbolTable)
}
c.mergeSymbolTable(inferred.Members, expando.Members, false, nil)
}
symbol = inferred
links = c.valueSymbolLinks.Get(inferred)
}
links.resolvedType = c.getTypeOfFuncClassEnumModuleWorker(symbol)
originalLinks.resolvedType = links.resolvedType
}
return links.resolvedType
}
Expand Down Expand Up @@ -16658,6 +16695,10 @@ func (c *Checker) getTypeOfPrototypeProperty(prototype *ast.Symbol) *Type {
}

func (c *Checker) getWidenedTypeForAssignmentDeclaration(symbol *ast.Symbol) *Type {
annotatedType := c.getAnnotatedTypeForAssignmentDeclaration(symbol)
if annotatedType != nil {
return annotatedType
}
var types []*Type
for _, declaration := range symbol.Declarations {
if ast.IsBinaryExpression(declaration) {
Expand Down Expand Up @@ -29713,3 +29754,35 @@ func (c *Checker) GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) pr
}
return c.emitResolver
}

func (c *Checker) getSymbolOfExpando(node *ast.Node) *ast.Symbol {
if node == nil || node.Parent == nil {
return nil
}
if ast.IsVariableDeclaration(node.Parent) && node.Parent.AsVariableDeclaration().Initializer == node {
if !ast.IsInJSFile(node) && !(ast.IsVarConstLike(node.Parent) && ast.IsFunctionLikeDeclaration(node)) {
return nil
}
return c.getSymbolOfDeclaration(node.Parent)
}
return nil
}

func (c *Checker) getAnnotatedTypeForAssignmentDeclaration(symbol *ast.Symbol) *Type {
if symbol.Parent == nil || symbol.Parent.ValueDeclaration == nil {
return nil
}
possiblyAnnotatedSymbol := c.getFunctionExpressionParentSymbolOrSymbol(symbol.Parent)
if possiblyAnnotatedSymbol == nil || possiblyAnnotatedSymbol.ValueDeclaration == nil || possiblyAnnotatedSymbol.ValueDeclaration.Kind == ast.KindFunctionDeclaration {
return nil
}
typeNode := possiblyAnnotatedSymbol.ValueDeclaration.Type()
if typeNode == nil {
return nil
}
annotationSymbol := c.getPropertyOfType(c.getTypeFromTypeNode(typeNode), symbol.Name)
if annotationSymbol == nil {
return nil
}
return c.getNonMissingTypeOfSymbol(annotationSymbol)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface StatelessComponent<P> {

const MyComponent: StatelessComponent<MyComponentProps> = () => null as any;
>MyComponent : StatelessComponent<MyComponentProps>
>() => null as any : { (): any; defaultProps: { color: "red"; }; }
>() => null as any : { (): any; defaultProps: Partial<MyComponentProps>; }
>null as any : any

MyComponent.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,3 @@
}

interface StatelessComponent<P> {
@@= skipped -11, +11 lines =@@

const MyComponent: StatelessComponent<MyComponentProps> = () => null as any;
>MyComponent : StatelessComponent<MyComponentProps>
->() => null as any : { (): any; defaultProps: Partial<MyComponentProps>; }
+>() => null as any : { (): any; defaultProps: { color: "red"; }; }
>null as any : any

MyComponent.defaultProps = {

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface N {
}
export const interfaced: N = () => true;
>interfaced : N
>() => true : { (): true; num: number; }
>() => true : { (): true; num: 123; }
>true : true

interfaced.num = 123;
Expand All @@ -21,7 +21,7 @@ interfaced.num = 123;
export const inlined: { (): boolean; nun: 456 } = () => true;
>inlined : { (): boolean; nun: 456; }
>nun : 456
>() => true : { (): true; nun: number; }
>() => true : { (): true; nun: 456; }
>true : true

inlined.nun = 456;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Combo {
}
const c: Combo = () => 1
>c : Combo
>() => 1 : { (): number; p: {}; }
>() => 1 : { (): number; p: { [s: string]: number; }; }
>1 : 1

// should not be an expando object, but contextually typed by Combo.p
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- old.typeFromPropertyAssignment30.types
+++ new.typeFromPropertyAssignment30.types
@@= skipped -8, +8 lines =@@
}
const c: Combo = () => 1
>c : Combo
->() => 1 : { (): number; p: {}; }
+>() => 1 : { (): number; p: { [s: string]: number; }; }
Copy link
Contributor Author

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"
>      : ^^^^^

>1 : 1

// should not be an expando object, but contextually typed by Combo.p