Skip to content

Instantly share code, notes, and snippets.

@denisos
Created November 28, 2022 05:28
Show Gist options
  • Save denisos/2063f3aa8ad76189b8cc2ad6c03764bc to your computer and use it in GitHub Desktop.
Save denisos/2063f3aa8ad76189b8cc2ad6c03764bc to your computer and use it in GitHub Desktop.
typescript cheatsheet hints
// typing functions
type FocusListener = (isFocussed: boolean) => void
const addListener = (onFocusChanged: FocusListener) => {
return true
}
// type object lietral map, can use
const cache: { [id: string]: string } = {
"1": "apples",
"2": "oranges"
};
const recordCache: Record<number, string> = {
1: "denis",
2: "jane"
}
// typing try catch
try {
throw "Just a string"
} catch(e: any) {
if (e instanceof Error) {
return e.message;
}
// do something with an e which is not Error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment