Skip to content

Instantly share code, notes, and snippets.

View jasonleehodges's full-sized avatar

jasonleehodges

View GitHub Profile
@jasonleehodges
jasonleehodges / models.ts
Last active February 13, 2022 23:01
Example of when to use Type Guards
export interface Noun {
id: string;
created?: number;
name: string;
}
export interface Character extends Noun {
gender: Gender;
}
@jasonleehodges
jasonleehodges / characterUndefined.ts
Created February 13, 2022 22:05
Optional Chaining and Nullish Coalescing Examples
const myCharacter: Character = api.get<Character>(`/character/${characterId}`);
if(myCharacter.metadata.type === 'Hero') {
// this will blow up if there is no "metadata" property on the "myCharacter" object
// Cannot read property 'type' of undefined
}
if(myCharacter?.metadata?.type === 'Hero') {
// the optional chaining operator will default the entire expression to undefined
// if any property along the way is undefined. This condition will evaluate to false
@jasonleehodges
jasonleehodges / declarations.d.ts
Created February 13, 2022 21:06
Example type declaration file
declare module 'exampleModule';
@jasonleehodges
jasonleehodges / ts7016.rtf
Created February 13, 2022 20:58
Could not find declaration file for module - ts(7016)
Could not find a declaration file for module 'exampleModule'. '.../node_modules/exampleModule.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/exampleModule` if it exists or add a new declaration (.d.ts) file containing `declare module 'exampleModule';`
@jasonleehodges
jasonleehodges / returnTypeExample.ts
Last active February 13, 2022 17:46
TS Return Type Error Message
function add(a: number, b: number): number {
/*
Let's assume there is a bunch of logic here which makes
the error message in this function less obvious
*/
return 'a' + 'b';
}
@jasonleehodges
jasonleehodges / ts-no-overload.rtf
Created February 9, 2022 05:27
TypeScript - No Overload Matches this call
No overload matches this call.
Overload 1 of 2, '(actionCreator: ActionCreatorWithPayload<Partial<Character>, string>, reducer: CaseReducer<State, { payload: Partial<Character>; type: string; }>): ActionReducerMapBuilder<...>', gave the following error.
Type '(WritableDraft<Partial<Character>> | "123")[]' is not assignable to type 'WritableDraft<Partial<Character>>[]'.
Type 'WritableDraft<Partial<Character>> | "123"' is not assignable to type 'WritableDraft<Partial<Character>>'.
Type '"123"' has no properties in common with type 'WritableDraft<Partial<Character>>'.
Overload 2 of 2, '(type: string, reducer: CaseReducer<State, { payload: Partial<Character>; type: string; }>): ActionReducerMapBuilder<State>', gave the following error.
Argument of type 'ActionCreatorWithPayload<Partial<Character>, string>' is not assignable to parameter of type 'string'.
@jasonleehodges
jasonleehodges / counterWithSelector.tsx
Last active December 11, 2021 19:40
Selector Example with Currying and Selector Unit Tests
import React from "react";
import { useSelector } from "react-redux";
import {selectCountFormatted} from './selectors'
export const Counter = () => {
const countFormattedAsDollars = useSelector(selectCountFormatted('$'));
...
@jasonleehodges
jasonleehodges / counterSelector.ts
Last active December 11, 2021 19:34
Counter Selector File
export const selectCount = (state: RootState) => state.counter.value;
export const selectCountFormatted =
(formatter: "$" | "%") => (state: RootState) =>
`${formatter === "$" ? "$" : ""}${selectCount(state)}${
formatter === "%" ? "%" : ""
}`;
export const selectCountAndStatus = (state: RootState) =>
`The current count is ${selectCount(state)} and the status is ${
state.counter.status
}`;
@jasonleehodges
jasonleehodges / inlineSelectorDefined.tsx
Last active December 11, 2021 20:22
Anti-pattern of an Inline Selector
export const Counter = () => {
const count = useSelector((state: RootState) => state.counter.value)
return (
<div>
...
@jasonleehodges
jasonleehodges / testableCounter.spec.ts
Last active December 11, 2021 18:31
Unit Tests for Reducer
import {
CounterState,
decrement,
increment,
incrementByAmount,
testableCounterReducer,
} from "./testableCounterReducer";
describe("testable counter reducer", () => {
const initialState: CounterState = {