Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to examine a type in typescript's repl (ts-node)?

So I have the following entered

type Duck = {
    colors: string;
    featheres: number;
}

type DuckProps = keyof Duck 

how do I examine/verify that for example DuckProps is of value: 'colors' | 'feathers'?

I can't seem to console log or use it as it will just return

[eval].ts:8:7 - error TS2693: 'DuckProps' only refers to a type, but is being used as a value here.

How do you interact with the typesript specific constructs (interface, type, etc.) via the repl? In other words, when i type Duck. I expect something to show up like this:

$ Duck

Duck<Typescript type> { color: string; feathers: number }
like image 954
user2167582 Avatar asked Oct 29 '25 07:10

user2167582


1 Answers

Here is a bit of a hack but gets the job done. Using the .type command we can coerce the type we are interested in into a statement and get ts-node to display the quick info associated with it.

> type Duck = {
...    colors: string;
...    featheres: number;
...  }
undefined
> type DuckProps = keyof Duck
undefined
> .type _ as DuckProps
type DuckProps = "colors" | "featheres"

Caveat: this only works with named types at the end. What happens underneath is that .type calls typescript's getQuickInfoAtPosition with the position at the end of the input. Like ctrl hover in the typescript playground, and that bottom grey line is what is displayed in addition to some documentation to go with it.

This seems like a useful feature from ts-node and might warrant a feature request.

like image 174
SpencerPark Avatar answered Oct 31 '25 01:10

SpencerPark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!