visual studio code - Editing Typescript file with VScode, how to hint type details of function parameters? - Stack Overflow

admin2025-04-17  2

I have:

type Foo = {n:number}
function test(f:Foo){}

When I type

test()

The popover shows "test(f: Foo): void". How can I let the popover shows "{n:number}".(I don't want to change the function definition.)

I have:

type Foo = {n:number}
function test(f:Foo){}

When I type

test()

The popover shows "test(f: Foo): void". How can I let the popover shows "{n:number}".(I don't want to change the function definition.)

Share Improve this question asked Feb 1 at 2:06 Elect2Elect2 1,5833 gold badges15 silver badges26 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Go to VSCode - Open User Settings (JSON) and paste in the following lines:

"typescript.inlayHints.enumMemberValues.enabled": true,
"typescript.inlayHints.functionLikeReturnTypes.enabled": true
"typescript.inlayHints.parameterNames.enabled": "all"

If you want intellisense to detect the internal workings of the type you should use a generic function to extend it

eg

type Foo = { n: number };

function test<Bar extends Foo>(f: Bar) {}

test({n: 0});

First, the way the test() function is being called above is an error and doesn't satisfy the required type { n: number; }

if you made it conditionally undefined such as

function test<Bar extends Foo>(f?: Bar) {}

then calling test() with no arguments would be okay

at any rate, extending the original type with a generic exposes the internal type workings on hover which I believe is what you're asking about

when I hover test({n:0}) using the following syntax

type Foo = { n: number };

function test<Bar extends Foo>(f: Bar) {}

test({n: 0});

typescript intellisense detects the following

function test<{
    n: number;
}>(f: {
    n: number;
}): void
转载请注明原文地址:http://anycun.com/QandA/1744841391a88368.html