Skip to content

RecursiveReadonly and RecursivePartial #18591

Closed
@NN---

Description

@NN---

Suggestion to add types to lib.d.ts:

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

type RecursivePartial<T> = {
    [P in keyof T]?: RecursivePartial<T[P]>;
};

Difference from Readonly and Partial

type ComplexType = { a: { b: { c: { d: number; } } } };

const x:Readonly<ComplexType> = {
    a: { b: { c: { d: 1 } } }
};
const xx: RecursiveReadonly<ComplexType> = {
    a: { b: { c: { d: 1 } } }
};
x.a.b.c.d = 3;
//xx.a.b.c.d = 3; // Error

const y:Partial<ComplexType> = {};
const yy: RecursivePartial<ComplexType> = {};

//y.a = {};// Error
yy.a = {}; // OK

Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions