Skip to content

Instantly share code, notes, and snippets.

@GeordieP
Last active June 25, 2021 12:52
Show Gist options
  • Save GeordieP/f3190d1f736cd9c195cb136acabf04a9 to your computer and use it in GitHub Desktop.
Save GeordieP/f3190d1f736cd9c195cb136acabf04a9 to your computer and use it in GitHub Desktop.
type Maybe<T> = T | null;
type BlogPost = {
title: string;
author: string;
}
type BlogPostResponse = Maybe<{
title?: Maybe<string>,
author?: Maybe<string>,
}>;
const myBlogPost: BlogPostResponse = {/*..*/};
myBlogPost?.title // Maybe<string> | undefined
//
// Type to unwrap the nested maybes into a concrete type
//
declare type Definitely<T> = NonNullable<{ [K in keyof T]-?: NonNullable<T[K]> }>;
const myBlogPost: Definitely<BlogPostResponse> = {/*..*/};
myBlogPost.title // string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment