1

I am trying to create a Type that only contains true fields.
I was able to achieve the wanted behavior with the new satisfies keyword but I am sure it has to be possible otherwise.

Example working with 'satisfies'

enter image description here

Without the 'satisfies' keyword it just gives me an empty type.

Example without 'satisfied'

enter image description here

Am I missing something here? In my brain it should just work.

type Fields = {
  [FieldName: string]: boolean,
}

type GetTrues<T> = {
  [K in keyof T as T[K] extends true ? K : never]: T[K]
}

const fields: Fields = {
  trueField: true,
  falseField: false
};

type myTrueFields = GetTrues<typeof fields> // expected {trueField: true} got {}

Pradeep Kumar
  • 1,193
  • 1
  • 9
  • 21
feissn
  • 11
  • 2
  • The answer relies on this : `type BooleanExtendsTrue = boolean extends true ? true : false;` – Eldar Nov 26 '22 at 09:39
  • 1
    Your `typeof fields` is not known-keyed, it's unreduced `Fields` – Dimava Nov 26 '22 at 11:26
  • See [this question](https://stackoverflow.com/questions/70956050/how-do-i-declare-object-value-type-without-declaring-key-type) and its answers for more information. – jcalz Nov 26 '22 at 20:35

0 Answers0