Skip to content

Proposal: Mapped Types syntax to remove modifiers #13723

Closed
@marcomura

Description

@marcomura

Current TypeScript Version: 2.1.1

We have a syntax to add "readonly" to every property of a type. Unfortunately, it seems there is no way to have the reverse operation: removing it.
This is useful for creating Mocked version of types for UTs, which have readonly fields in production.

This should work recursively.

Code

interface MyType2 {
  readonly z1: number;
  z2: string;
}

interface MyType {
  readonly a: number;
  readonly b: MyType2;
  c: string;
  d: MyType2;
}

type Mock<T> {
  [P in keyofwithoutmodifiers T]: Mock<T[P]>;
}

type MyMockedType = Mock<MyType>;

Expected behavior:
type MyMockedType to be

{
  a: number;
  b: { z1: number; z2: string };
  c: string;
  d: { z1: number; z2: string };
}

Actual behavior:
not possible right now

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions