05181-hard-mutable-keys

Back

type MutableKeys<T extends object> = keyof {
  [K in keyof T as Equal<{ [Keys in K]: T[Keys] }, { -readonly [Keys in K]: T[Keys] }> extends true ? K : never]: unknown
}

Solution by ouzexi #34306

type MutableKeys<
  T extends object,
  M extends object = MutableObject<T>,
  K extends keyof T = keyof T
> = K extends keyof T
  ? Equal<Pick<T, K>, Pick<M, K & keyof M>> extends true
    ? K
    : never
  : never;

type MutableObject<T> = {
  -readonly [P in keyof T]: T[P];
};

Solution by Vampirelee #32645

type MutableKeys<T, S extends keyof T = keyof T> = S extends unknown
  ? Equal<{ readonly [K in S]: T[K] }, Pick<T, S>> extends true
    ? never
    : S
  : never;

Solution by vangie #32247

type MutableKeys<T extends object> = {
     [P in keyof T] -?: Equal<{[Q in P]:T[Q]},{-readonly [Q in P]: T[Q]}> extends true ? P : never
}[keyof T]

Solution by idebbarh #31411

type MutableKeys<T> = keyof {
  [K in keyof T as Equal<Pick<T, K>, Readonly<Pick<T, K>>> extends true ? never : K]: T[K]
}

Solution by dreamluo-plus #30777

type MutableKeys<T extends object> = keyof {
    [key in keyof T as Equal<Pick<T, key>, Readonly<Pick<T, key>>> extends true ? never : key]: T[key]
}

Solution by zhuizhui0429 #25877

type MutableKeys<T extends object> = keyof { [P in keyof T as Equal<{ [K in P]: T[K] }, { -readonly [K in P]: T[K] }> extends true ? P : never]: any };

Solution by E-uler #24984

// your answers
type IsReadonlyKey<T, K extends keyof T> = Equal<{[P in K]: T[P]} , {-readonly[P in K]:T[P]}> extends false ? true : false

type GetKeys<T> = keyof T

type MutableKeys<T> = GetKeys<{
  [K in keyof T as IsReadonlyKey<T,K> extends true ? never : K] : K
}>

Solution by snakeUni #24028

type IsEqual<X, Y> =
  (<T>() => T extends X ? 1 : 2) extends
  (<T>() => T extends Y ? 1 : 2) ? true : false

type MutableKeys<T> =  {
  [K in keyof T]: IsEqual<Pick<T, K>, Readonly<Pick<T, K>>> extends true ? never : K
}[keyof T]

Solution by TKBnice #23371

type IsEqual<A, B> = 
  (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2)
  ? true 
  : false

type IsReadonlyKey<O, K extends keyof O, _OPK = Pick<O, K>> = IsEqual<_OPK, Readonly<_OPK>>

type ReadonlyKeys<T, K extends keyof T = keyof T> = 
  K extends unknown
  ? IsReadonlyKey<T, K> extends true ? K : never
  : never

type MutableKeys<T> = Exclude<keyof T, ReadonlyKeys<T>>

playground

Solution by zhaoyao91 #21954

// your answers
type IsReadonlyKey<T, K extends keyof T> = Equal<{[P in K]: T[P]} , {-readonly[P in K]:T[P]}> extends false ? true : false

type GetKeys<T> = {
  [K in keyof T] : T[K]
}[keyof T]

type MutableKeys<T> = GetKeys<{
  [K in keyof T as IsReadonlyKey<T,K> extends true ? never : K] : K
}>

Solution by YqxLzx #21650

// your answers
type IsReadonlyKey<T extends Record<keyof any, any>, K extends keyof T> = Equal<{[P in K]: T[P]} , {-readonly[P in K]:T[P]}> extends false ? true : false
type res  = IsReadonlyKey<{readonly a : number}, 'a'>


type MutableKeys<T extends Record<keyof any, any>> = {} extends T ? never : keyof {
  [K in keyof T as IsReadonlyKey<T, K> extends true ? never: K]: K
} 

Solution by Rebornjiang #21399

type UnionToCross1<G> = {
  [P in G extends any ? keyof G : any]: G extends any
    ? P extends keyof G
      ? G[P]
      : never
    : never;
} & {};

type MutableKeys<
  T extends Record<string, any>,
  G extends keyof T = keyof T
> = keyof UnionToCross1<
  G extends any
    ? Equal<
        {
          readonly [P in G]: T[P];
        },
        Pick<T, G>
      > extends true
      ? never
      : Pick<T, G>
    : never
>;

Solution by so11y #21253

type MutableKeys<T> = keyof {
  [P in keyof T as Equal<Pick<T, P>, Readonly<Pick<T, P>>> extends true ? never : P]: 1
}

Solution by lvjiaxuan #20334

// your answers
type IsEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends <
  T1
>() => T1 extends B ? 1 : 2
  ? true
  : false;
type MutableKeys1<T> = keyof {
  [key in keyof T as IsEqual<
    Readonly<{ [p in key]: T[p] }>,
    { [p in key]: T[p] }
  > extends true
    ? never
    : key]: T[key];
};

Solution by fengjinlong #20332

type MutableKeys<T> = keyof {
  [key in keyof T as Equal<Readonly<{ [p in key]: T[p] }>, { [p in key]: T[p] }> extends true ? never : key]: T[key];
};

Solution by CaoXueLiang #19484

type Equal<XX, YY> = (() => T extends XX ? 1 : 2) extends () => T extends YY ? 1 : 2 ? true : false; type MutableKeys = keyof { [K in keyof T as Equal<Pick<T, K>, Readonly<Pick<T, K>>> extends true ? never : K]: T[K] };

Solution by my5201314zwl #16859

// your answers

type MutableKeys<T> = keyof {
  [P in keyof T as Equal<{ [K in P]: T[K] }, Readonly<{ [K in P]: T[K] }>> extends false ? P: never]: T[P]
}

Solution by humandetail #16495

// your answers
type MutableKeys<
    T extends object > =
    keyof 
    {[J in keyof T as Equal<Readonly<{[K in J]:T[K]}>,{[K in J]:T[K]}> extends true ? never : J] : T[J]}  ;

Solution by justBadProgrammer #15999

type IsReadOnly<T, P extends keyof T> = Equal<Pick<T, P>, {readonly [K in P]: T[P]}>

type MutableKeys<T> = keyof {
  [P in keyof T as IsReadOnly<T, P> extends true ? never: P]: T[P]
}

Solution by sromic #15851

// your answers
type IsEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false

type MutableKeys<T extends Record<string, unknown>> = keyof {
  [K in keyof T as IsEqual<Pick<T, K>, Readonly<Pick<T, K>>> extends true ? never : K ]: T[K]
}

Solution by liuxing95 #13127

type IsEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false

type MutableKeys<T> = keyof {
  [K in keyof T as  IsEqual<Pick<T, K>, Readonly<Pick<T, K>>> extends true ? never : K]: K
} 

Solution by XkSuperCool #12964

type MutableKeys<T> =
  keyof {
    [
    K in keyof T as
    MyEqual<{ [P in K]: T[P] }, { readonly [P in K]: T[P] }> extends true ? (
      never
    ) : K
    ]: T[K]
  }

type MyEqual<A, B> = (<T>() => T extends A ? 1 : 0) extends (<T>() => T extends B ? 1 : 0) ? true : false

Playground

Solution by teamchong #11771

type MutableKeys<T, K = keyof T> = K extends keyof T
  ? Equal<Pick<T, K>, Readonly<Pick<T, K>>> extends true
    ? never
    : K
  : never

Solution by adnasa #10712

type IsEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
type MutableKeys<T> = {
  [K in keyof Required<T>]: IsEqual<Pick<T, K>, Readonly<Pick<T, K>>> extends true ? never : K
}[keyof T];

Solution by XkSuperCool #10269

// your answers
type MutableKeys<T, K = keyof T> = K extends keyof T
  ? Equal<Pick<T, K>, Readonly<Pick<T, K>>> extends true
    ? never
    : K
  : never

Here I used the Equal type provided by utilities, whose implementation can be found here.

Solution by DaKoala #6255

type MyEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false

type MutableKeys<T> = keyof {
  [Key in keyof T as MyEqual<Pick<T, Key>, Readonly<Pick<T, Key>>> extends true ? never : Key ]: any
}

Solution by Sun79 #5221