weikee94 / react-ts

react with typescript
0 stars 0 forks source link

Common Types #1

Open weikee94 opened 3 years ago

weikee94 commented 3 years ago

Common Types

type CounterProps = {
    incident: string;
    count: number;
    enabled: boolean;
}

type GroceryListProps = {
    items: string[];
    status: 'loading' | 'error' | 'success';
}

type ContrivedExampleComponentProps = {
    anObject: object; // useful as a placeholder
    anotherObject: {}; // can have any properties and values
    item: {
        id: string;
        title: string;
    };
    items: {
        id: string;
        title: string;
    }[]; // An array of objects of a certain shape
}

type Item = {
    id: string;
    title: string;
}

type ContrivedExampleComponentProps2 = {
    item: Item;
    items: Item[];
}

type ItemHash = {
    [key: string]: Item;
}

type Dictionary = {
    [key: number]: string;
}

type ContrivedExampleProps = {
    onHover: () => void;
    onChange: (id: number) => void;
    onClick(event: React.MouseEvent<HTMLButtonElement>): void;
}

const add = (a: number, b: number): number => {
    return a + b;
}

function substract(a: number, b: number): number {
    return a - b;
}

type ContrivedProps = {
    requiredProp: boolean;
    optionalProp?: string;
}
aaronangxz commented 3 years ago

cool TypeScript