Skip to content

Infering type from usage does silly import #32910

Closed

Description

@jpike88

TypeScript Version: typescript@3.6.0-dev.20190814

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.

Activity

  1. DanielRosenwasser commented on Aug 15, 2019

    @DanielRosenwasser
    Member

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

  2. andrewbranch commented on Aug 15, 2019

    @andrewbranch
    Member

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

  3. jpike88 commented on Aug 20, 2019

    @jpike88
    Author

    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.

  4. fatcerberus commented on Aug 20, 2019

    @fatcerberus

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

  5. andrewbranch commented on Sep 16, 2019

    @andrewbranch
    Member

    Josh Pike (@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.

  6. jpike88 commented on Sep 19, 2019

    @jpike88
    Author

    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.

  7. andrewbranch commented on Oct 4, 2019

    @andrewbranch
    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

Metadata

Metadata

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions