1

I'm trying to create a tooltip in IDE with all keys of a provided type. In a simplified version the implementation looks like this:

type A = {
  foo: string
  bar: string
  baz: string
}

type KeysA = `Keys are ${keyof A}`

But KeysA in the tooltip looks like "Keys are foo" | "Keys are bar" | "Keys are baz". Is this possible to create a tooltip looking like Keys are "foo" | "bar" | "baz" using keyof A?

Sanitysign
  • 11
  • 2
  • You don’t just want to prevent distribution; you’re asking to serialize a union in some order, but union ordering is intended to be unobservable in TS. There are some nasty solutions which will do what you’re asking for but I’d strongly suggest you rethink the approach entirely… this feels like an XY problem; why wouldn’t just `type KeysA = keyof A` work if the goal is an IDE tooltip? Why would you need to doubly serialize types? I guess I’m wondering what the underlying use case is that you’re trying to address. – jcalz Jun 04 '23 at 13:18
  • See [this q and a](https://stackoverflow.com/q/55127004/2887218). If I use that technique here you get [this result](https://tsplay.dev/mM7Elw) but again: strongly suggest you rethink this approach because it's fragile. Anyway does that fully address the question? If so I'll write up an answer (or possibly close as duplicate of the other question about serializing a union). If not, what am I missing? – jcalz Jun 04 '23 at 14:06
  • 1
    Huge thanks for the explanation! I thought I was missing some simple solution for this problem. Your answer clearly shows that I need to abandon the idea of such tooltip – Sanitysign Jun 05 '23 at 16:17

0 Answers0