Skip to content

Infering type from usage does silly import #32910

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
jpike88 opened this issue Aug 15, 2019 · 7 comments · Fixed by #33915
Closed

Infering type from usage does silly import #32910

jpike88 opened this issue Aug 15, 2019 · 7 comments · Fixed by #33915
Assignees
Labels
Domain: Auto-import Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue

Comments

@jpike88
Copy link

jpike88 commented Aug 15, 2019

TypeScript Version: [email protected]

myInterfaceFile.ts

export interface MyExportedInterface {
	something: number
}

another file, when using 'infer type from usage' option in VSCode

export async function someFunction(metricParams: { algebra: import("../../../../../../../../../blaa/bla/myInterfaceFile.ts").MyExportedInterface }) {

}

An 'import' function shouldn't be called.

@DanielRosenwasser DanielRosenwasser added Domain: Auto-import Paths in Generated import(...)s Poor Paths in import(...) types. Experience Enhancement Noncontroversial enhancements and removed Paths in Generated import(...)s Poor Paths in import(...) types. labels Aug 15, 2019
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.7.0 milestone Aug 15, 2019
@DanielRosenwasser
Copy link
Member

We should always prefer an import statement at the top of the file.

@andrewbranch
Copy link
Member

@jpike88 can you share the contents of that file pre-“infer type from usage” command?

@jpike88
Copy link
Author

jpike88 commented Aug 20, 2019

To reproduce, the file is empty other than the function I've defined, including not having any import statements.

export async function Test(test, user) {

}

Whats weird is this:

if I tell it to infer parameter types, I get this:


export async function Test(test: any, user: any) {

}

Which is correct I guess.

But if the function contains just one line of code, I get this:


export async function getPlantMetric(test: any, user: import("../../../../../../../../../project/some/path//shared/models/User").User) {
	if (!test) throw new Error('whatever!');
}

The second argument attempts to get at an import, but in doing so incorrectly inlines an import() function instead of pulling it in at the top.

@fatcerberus
Copy link

How is it even arriving at that type for user if the only thing in the function is just an unconditional throw?

@andrewbranch
Copy link
Member

@jpike88 I can’t reproduce this with the info provided. I’m sure it must depend somewhat upon the file that exports some User type, and possibly upon project settings. Can you create or give me access to a repo that exhibits the issue, or describe more details about how to repro? Assume all details are relevant unless you can prove them otherwise.

@andrewbranch andrewbranch added the Needs More Info The issue still hasn't been fully clarified label Sep 16, 2019
@jpike88
Copy link
Author

jpike88 commented Sep 19, 2019

Sorry don't have the time, and I guess is an edge case.

But I think that there should be a VSCode setting that disables the import() function from being used at all when these sorts of judgements are being made.

@andrewbranch
Copy link
Member

How is it even arriving at that type for user if the only thing in the function is just an unconditional throw?

Somewhere else in the project, something is calling that function with an argument of type User. The code that triggers this issue isn’t shown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Auto-import Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants