Skip to content

Instantly share code, notes, and snippets.

@jeromecovington
Last active January 11, 2021 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeromecovington/f46e6dea4b37622af078c9adef935231 to your computer and use it in GitHub Desktop.
Save jeromecovington/f46e6dea4b37622af078c9adef935231 to your computer and use it in GitHub Desktop.
type PartiallyRequired<T, K extends keyof T> = Pick<T, K> & Partial<T>;
type Test = {
foo: string;
bar: string;
baz: string;
};
type Test1 = PartiallyRequired<Test, "foo">;
// Will error if foo is not included. bar, baz optional.
const test1: Test1 = {
foo: '',
bar: '',
baz: ''
}
type Test2 = PartiallyRequired<Test, "bar" | "baz">;
// Both bar and baz must be included. foo optional.
const test2: Test2 = {
foo: '',
bar: '',
baz: ''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment