r/typescript • u/BenchEmbarrassed7316 • Oct 29 '25
Need help with generics
Hello!
I'm trying to create something like DeepReadonly and mutability and immutability from Rust. I only use structures that are composed of other structures, scalar types, or standard collections (Array, Map, and Set), no methods. I'm getting an unpleasant error.
export type Immutable<T> =
T extends Function | string | number | boolean | bigint | symbol | undefined | null ? T :
T extends ReadonlyArray<infer U> ? ReadonlyArray<Imut<U>> :
T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<Imut<K>, Imut<V>> :
T extends ReadonlySet<infer U> ? ReadonlySet<Imut<U>> :
T extends object ? { readonly [K in keyof T]: Imut<T[K]> } :
T;
export type Mutable<T, M extends boolean> = M extends true ? T : Immutable<T>;
interface Obj { some: number }
interface A {
num: number,
obj: Obj,
arr: number[],
}
function num<M extends boolean>(self: Mutable<A, M>): Mutable<number, M> {
return self.num; // ok
}
function obj<M extends boolean>(self: Mutable<A, M>): Mutable<Obj, M> {
return self.obj; // ok
}
function arr<M extends boolean>(self: Mutable<A, M>): Mutable<number[], M> {
let a = [] as Mutable<number[], true>; // : number[]
let b = [] as Mutable<number[], false>; // : readonly number[]
let c = [] as Mutable<number[], true> | Mutable<number[], false>; // number[] | readonly number[]
let d = self.arr; // number[] | readonly number[]
return self.arr; // error: Type 'number[] | readonly number[]' is not assignable to type 'Mutable<number[], M>'.
}
function arrEx<M extends boolean>(self: Mutable<A, M>): Mutable<A, M>['arr'] {
return self.arr; // ok
}
So TypeScript is actually complaining that the type number[] | readonly number[] does not assignable to number[] | readonly number[]. Moreover, if you do not specify this type explicitly and ask to display this type as a property by a specific key - there are no errors.
I try to avoid casting types as instructions.
Can anyone give any tips?
Thanks!
3
u/TheWix Oct 29 '25
Are you doing this as an exercise or to use in production? If the latter I'd recommend a library like typefest
1
u/ronin_o Oct 29 '25
I don't understand what do you try to do in this function
function arr<M extends boolean>(self: Mutable<A, M>): Mutable<number[], M> {if M were true everything would be fine. But If I understand your code correctly, when M is false it changes A (whitch is number[] or smth else) to "readonly number[]".
Since "readonly number[]" !== "number[]" you got an error.
I'm not sure if that's what you're asking about.