Typescript auto-fix for mismatched types

When interacting with third-party libraries, I frequently don't know the required type I need:

 

function myFunction(x: ???) {
 libraryFunction(x)
}

If I type `x` incorrectly (say, as a string), Typescript will rightly complain, and will tell me what type is expected by `libraryFunction`:

TS2322: Type 'string' is not assignable to type 'ExpectedType'.

But it's often very non-obvious where `ExpectedType` should be imported from. Obviously Typescript / Webstorm knows where it is, but the error message does not tell me where to import it from, nor does the `More actions ...` menu offer to fix the problem.

This happens to me all the time. As it stands I can usually find the required by clicking through enough definitions or sometimes grepping through `node_modules`, but it's always a pain.

Could the error message include an option to `Fix type`? (This would mean importing the correct type at the top of the file and then replacing `x: string` with `x: ExpectedType`.) Failing that, could the message at least tell me what file / path the type resides in so I can import it?

0
1 comment

You can open the quick doc of a library function - ExpectedType is normally clickable there:

click it to open its docs, then use F4 to jump to type definition:

0

Please sign in to leave a comment.