Skip to content
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

getType returns wrong type in property access expression in JS #1525

Open
steinybot opened this issue Apr 10, 2024 · 4 comments
Open

getType returns wrong type in property access expression in JS #1525

steinybot opened this issue Apr 10, 2024 · 4 comments

Comments

@steinybot
Copy link

steinybot commented Apr 10, 2024

Describe the bug

Version: 22.0.0

To Reproduce

import {Project, ScriptKind, SyntaxKind} from "ts-morph";

const project = new Project({});

// This works.
//const className = "Thing"
// This doesn't work.
const className = "Image"

const ts = project.createSourceFile("test.d.ts",
    `
    declare class ${className} {}
    `, { scriptKind: ScriptKind.TS })
const file = project.createSourceFile("test.js",
    `
    class ${className} {}
    ${className}.foo = 'bar'
    `, { scriptKind: ScriptKind.JS })

const statement = file.getStatementByKind(SyntaxKind.ExpressionStatement)
if (statement) {
    const binExp = statement.getChildAtIndexIfKindOrThrow(0, SyntaxKind.BinaryExpression)
    const propAccessExp = binExp.getChildAtIndexIfKindOrThrow(0, SyntaxKind.PropertyAccessExpression)
    const imageId = propAccessExp.getChildAtIndexIfKindOrThrow(0, SyntaxKind.Identifier)
    console.log(imageId.getType().getText())
}

Reproduction: https://github.com/steinybot/bug-reports/tree/ts-morph/wrong-get-type

Expected behavior

It should log typeof Image but it logs new (width?: number, height?: number) => HTMLImageElement

@lazarljubenovic
Copy link
Contributor

Pretty sure you're augmenting a globally available Image via some defaultly available DOM types.

@steinybot
Copy link
Author

Pretty sure you're augmenting a globally available Image via some defaultly available DOM types.

AFAIK the locally defined Image should shadow the global one. That's what it seems to do when I run the code.

This is a minimal reproduction from real code in @pulumi/aws which I don't think does the correct thing at runtime although I haven't checked this specific instance.

@steinybot
Copy link
Author

I did some debugging and it gets to resolveNameHelper in the type checker and it traverses up to the SourceFileObject. canHaveLocals returns true but the location.locals is empty for some reason. It looks like if it wasn't empty then it would attempt to lookup from these locals first. Only if it doesn't find it here does it look for it in the globals.

@steinybot
Copy link
Author

Although even if locals was not empty it fails the !isGlobalSourceFile(location) check. Perhaps I need to get it to treat it as a ModuleDefinition instead? Not sure how to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants