Question about typing decorators in TypeScript

267 Views Asked by At

Is it possible in TypeScript to implement a decorator @important and a utility type PickImportantProps in a way that type B becomes { b: string } in the following example? Many thanks in advance :)


// decorator
function important(...): ... {
  ...
}

type PickImportantProps<T> = ...

class A {
  a = 'a'

  @important()
  b = 'b'
}

type B = PickImportantProps<A> // <= should result in { b: string }
1

There are 1 best solutions below

0
Marian On BEST ANSWER

At the moment it isn't possible to get this information at compile-time.

The answer to this question shows a possible workaround that essentially gives up the decorator syntax (@decoratorName).