Skip to content

Should we be able to provide an enum when using omit? #40944

@LiamDotPro

Description

@LiamDotPro

Search Terms

Omit Enums, Can I use Enums with Omit utility types

Suggestion

I would like to be able to pass in an enum to the utility type Omit as the second argument.

Use Cases

Currently you can only provide strings when using an Omit I would like to also be able to provide an Enum so I can notify future developers of the keys that are allowed to be Omitted. It would also make defining different types simpler because I can use code completion without having to fully type out the string.

Examples

interface Todo {
  title: string;
  description: string;
  completed: boolean;
}

enum TodoPossibleKeys {
    TITLE='title',
    DESCRIPTION='description'
}

type TodoPreview = Omit<Todo, TodoPossibleKeys.DESCRIPTION>;

const todo: TodoPreview = {
  title: "Clean room",
  completed: false,
};

todo;

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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