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 logStatus(status: "LOADING" | "LOADED") { | |
console.log(status); | |
} | |
const Status = { | |
Loading: "LOADING", | |
Loaded: "LOADED", | |
}; | |
logStatus(Status.Loading); // 💥 - Type error - Argument of type 'string' is not assignable to parameter of type '"LOADING" | "LOADED"' |
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; | |
scores: number[]; | |
} | |
const people: Person[] = [ | |
{ id: 1, name: "Bob", scores: [50, 45] }, | |
{ id: 2, name: "Jane", scores: [70, 60] }, | |
{ id: 3, name: "Paul", scores: [40, 75] } | |
] |
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 createGetPersonAction() { | |
return { type: 'GetPerson' } as const; | |
} | |
const getPersonAction = createGetPersonAction(); // `getPersonAction` is of type `{ readonly type: "GetPerson"; }` |
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 = { | |
readonly name: string; | |
readonly scores: ReadonlyArray<number>; // same as readonly 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 = { | |
readonly name: string; | |
readonly scores: readonly number[]; | |
} | |
const bob: Person = { | |
name: "Bob", | |
scores: [50, 45] | |
} | |
bob.scores.push(60); // 💥 - Type error - Property 'push' does not exist on type 'readonly 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 = { | |
readonly name: string; | |
readonly scores: number[]; | |
} | |
const bob: Person = { | |
name: "Bob", | |
scores: [50, 45] | |
} | |
bob.scores.push(60); // does this raise a type error? |
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 person: unknown = await getPerson(id); | |
if (isPersonWithAddress(person)) { | |
const postcode = person.address.postcode; | |
} | |
function isPersonWithAddress(person: any): person is Person { | |
return "address" in person && "postcode" in person.address; | |
} |
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 person: any = await getPerson(id); | |
const postcode = person.address.postcode; // TypeScript doesn't yell at us but what if there is no address property ... 💥! |
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 [state, setState] = useState(initialState); |
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 Reset = { | |
type: "reset"; | |
to: number; | |
}; | |
type Actions = Increment | Decrement | Reset; |