This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { useQuery, useMutation } from "react-query"; | |
type Person = { | |
id: number; | |
firstName: string; | |
lastName: string; | |
}; | |
async function getPerson({ queryKey }: { queryKey: [string, { id: number }] }) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
type Character = { | |
name: string; | |
}; | |
interface PromiseWithCancel<T> extends Promise<T> { | |
cancel: () => void; | |
} | |
function getCharacter(id: number) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Person = { | |
id: number; | |
name: string; | |
mobile: string; | |
email: string; | |
age: number; | |
} | |
// PersonWithoutContactDetails automatically becomes { id: number; name: string; age: number;} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type PersonWithoutContactDetails = Omit<Person, "mobile" | "email"> // { id: number; name: string; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Person = { | |
id: number; | |
name: string; | |
mobile: string; | |
email: string; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPersonScore(id: number): number { | |
const person: Person = getPerson(id); | |
return person.score ?? 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Person = { | |
id: number; | |
name: string; | |
score?: number; | |
} | |
function getPersonScore(id: number): number { | |
const person: Person = getPerson(id); | |
return person.score; // 💥 - Type error - Type 'number | undefined' is not assignable to type 'number' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPersonPostcode(id: number) { | |
const person: Person = getPerson(id); | |
return person.address?.zipcode; // returns `undefined` if `address` is `undefined` | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Person = { | |
id: number; | |
name: string; | |
address?: Address; | |
} | |
type Address = { | |
line1: string; | |
line2: string; | |
line3: string; | |
zipcode: string; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Status = { | |
Loading: "LOADING", | |
Loaded: "LOADED", | |
} as const ; | |
logStatus(Status.Loading); // Type is "LOADING" |
NewerOlder