简体   繁体   中英

How to copy the inferred type of an untyped function (TypeScript) in Visual Studio Code when its very long

(See Screenshot) 鼠标悬停时功能截图

I have this function with a really long untyped object returned, I want to copy the inferred type of that object because it's way too long to do manually, and then I want to make class/interface of it, example:

export class PatientTables {
  emsf: string;
  cadNumber: string;
  // and so on
}

is there a way to copy it from somewhere as it seems Visual Studio Code already has it assorted somewhere, as you can see in the picture (48 more), how can I make it show the rest and copy it? thanks!

Right click the function name, choose "refactor", then "Infer function return type".

One approach is to use the tsc command line tool. If you can isolate the file so that there are no dependencies, then you can run:

tsc filename.ts --declaration --emitDeclarationOnly

The resulting filename.d.ts file will include the generated type.

Use this extension (JSON to TS).

This doesn't directly answer your question, but I think it solves your problem. The extension takes JSON and converts it into TypeScript interfaces.

在此处输入图像描述

You can get the type from the following code:

function createPatientTables() {
    return {
        foo: 'hello world',
        bar: 123
    };
}

type PatientTables = ReturnType<typeof createPatientTables>;

RMC refactor to named function first, then RMC on new function Refactor shows infer return type option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM