#T extends undefined ? undefined | null only works for types

14 messages · Page 1 of 1 (latest)

tired obsidian
#

Hi I have this type

export type RecursivelyAddNullsForOverwriting<T> = T extends undefined
  ? undefined | null
  : T extends (infer U)[]
  ? RecursivelyAddNullsForOverwriting<U>[]
  : T extends Record<string, unknown>
  ? { [K in keyof T]: RecursivelyAddNullsForOverwriting<T[K]> }
  : T;

If I pass a type in for T then it works, but if I pass an interface in for T it does not apply then null.

Am I missing something here?

wooden fiber
tired obsidian
#

aha! hmm, how should I do it?

wooden fiber
#

maybe try extends object instead

flint zenith
#

extends Record<PropertyKey, any> works as well

wooden fiber
#

so it does, TIL

flint zenith
#

Record<string, any> to some extend, but won't work for types that have other kinds of keys, like numbers and symbols

wooden fiber
#

oh but actually object is probably more appropriate here

#

don't wnat to accidentally expand primitives like string after all

tired obsidian
#

I don' thave any keys that are other than string. but if Record<PropertyKey, any> works then that's awesome

#

hmmm, not sure what expanding string means or how that would happen

#

Ok, good stuff! Thanks folks!

wispy pawnBOT
#
type Expand<T> = { [K in keyof (T & {})]: (T & {})[K]; }
type Test = Expand<string>;
//   ^? - type Test = {
//       [x: number]: string;
//       toString: () => string;
//       charAt: (pos: number) => string;
//       charCodeAt: (index: number) => number;
//       concat: (...strings: string[]) => string;
//       indexOf: (searchString: string, position?: number | undefined) => number;
//       ... 35 more ...;
//       [Symbol.iterator]: () => IterableIterator<...>;
//   }
wooden fiber
#

!close