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

(See Screenshot) Screenshot of function when hovering with mouse

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!



Solution 1:[1]

You can get the type from the following code:

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

type PatientTables = ReturnType<typeof createPatientTables>;

Solution 2:[2]

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.

Solution 3:[3]

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

Solution 4:[4]

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.

enter image description here

Solution 5:[5]

RMC (Right mouse Click) refactor to named function first, then RMC on new function Refactor shows infer return type option.

Solution 6:[6]

For me; the Visual Code option was not there in the ?. So I visited the Typescript playground and copy-pasted the Javascript object for which I needed the types. For ex: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAHjAvDA3gWAFA2zAZiEALhgHJ8QSAaTHGAIwEMAnYkxpkzAXyA

This is how it looks:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xinan
Solution 2 mrcrowl
Solution 3 ZYinMD
Solution 4 jrasm91
Solution 5
Solution 6 Aakash