TypeScript: Type information doesn't always show up, even though TS has it

I've noticed various cases similar to this one, often when using mapped typed, unfortunately I don't have a minimal example:

Notice that `message.p` suggest `payload` with type `Field`. This is correct. However once I select `.payload` I get this behavior:

http://g.recordit.co/BOBvxhKlhS.gif

 

  • Mousing over `message.payload` doesn't give me the type (holding Cmd)
  • Mousing over the assinged `foo` incorrect says the type is `any`

This makes it hard to tell in the IDE what's going on, although the TS compiler knows. Especially since cmd-clicking `payload` (go to source) only takes you to the mapped type expression, not where the type is derived from, so you basically can't tell what the type is. Yet the original type suggestion shows it correctly.

 

3 comments
Comment actions Permalink

Please can you provide files/code snippets that can be used to recreate the issue?

0
Comment actions Permalink

This probably isn't the minimal example but it's the case that I'm seeing this issue with (actual usage has a lot more classes and methods across multiple modules):

portfolioService.ts

export type Portfolio = { id: string; name: string; }

export class PortfolioApi {
getPortfolio(): Promise<Portfolio> {
return Promise.resolve({ id: "0", name: "foo" });
}
getPortfolios(): Promise<Array<Portfolio>> {
return Promise.resolve([{ id: "0", name: "foo" }]);
}
}

export const getPortfolioApi = () => new PortfolioApi();

main.ts

import * as portfolioServiceApi from "./portfolioService";

type AllApiMessagesOfModule<T> = {
[K in keyof T]: T[K] extends new (...args: any[]) => infer C
? AllMethodMessagesOfApiClass<C>
: never;
}[keyof T];

type AllMethodMessagesOfApiClass<C> = {
[K in keyof C]: C[K] extends (...args: any[]) => Promise<infer R>
? { type: K; payload: R }
: never;
}[keyof C];

type AllMessages = AllApiMessagesOfModule<typeof portfolioServiceApi>;

declare const message: AllMessages;
if (message.type == "getPortfolio") {
const foo = message.payload;
}

If I mouse over `foo` I get `const foo: any` but the type is `Portfolio`. Compiler will flag this correctly, but IDE doesn't give me the correct type feedback. For example type suggestions don't work right.

 

 

0
Comment actions Permalink

thanks! Logged as https://youtrack.jetbrains.com/issue/WEB-33161, please follow it for updates

0

Please sign in to leave a comment.