Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Omit<Type, Keys> #7

Open
nmsn opened this issue Dec 5, 2022 · 2 comments
Open

Omit<Type, Keys> #7

nmsn opened this issue Dec 5, 2022 · 2 comments

Comments

@nmsn
Copy link
Contributor

nmsn commented Dec 5, 2022

Constructs a type by picking all properties from Type and then removing Keys (string literal or union of string literals).

interface Todo {
  title: string;
  description: string;
  completed: boolean;
  createdAt: number;
}
 
type TodoPreview = Omit<Todo, "description">;
 
const todo: TodoPreview = {
  title: "Clean room",
  completed: false,
  createdAt: 1615544252770,
};
 
todo;
// const todo: TodoPreview
 
type TodoInfo = Omit<Todo, "completed" | "createdAt">;
 
const todoInfo: TodoInfo = {
  title: "Pick up kids",
  description: "Kindergarten closes at 5pm",
};

todoInfo;
// const todoInfo: TodoInfo
@nmsn nmsn added the TypeScript label Dec 5, 2022
@nmsn
Copy link
Contributor Author

nmsn commented Dec 5, 2022

/**
 * Construct a type with the properties of T except for those in type K.
 */
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

@nmsn
Copy link
Contributor Author

nmsn commented Dec 6, 2022

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant