Skip to content

Instantly share code, notes, and snippets.

View frivolta's full-sized avatar

Filippo Rivolta frivolta

View GitHub Profile
@frivolta
frivolta / tsconfig.json
Created January 8, 2020 22:21
TsConfig Boilerplate
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"removeComments": true,
"noImplicitAny": true,
// Define a book type
type Book = {
title: string;
genre: string;
publicationYear: number;
};
// Define a list of types
const listOfBooks: Book[] = [
{
// Define a book type
type Book = {
title: string;
genre: string;
publicationYear: number;
};
// Define a list of types
const listOfBooks: Book[] = [
{title: 'Dragon Of The Titans', genre: 'fantasy', publicationYear: 1992},
// Let's define our valueToFilter type
const filterArrayByValue = <T, P extends keyof T>(
items: T[],
propertyName: P,
valueToFilter: T[P] //Partial<T>
): T[] => {
return items.filter(item =>item[propertyName] === valueToFilter);
};
type Book = {
title: string;
genre: string;
publicationYear: number;
};
type Car = {
modelName: string;
type: string;
price: number;
// Add a new generic type P
const filterArrayByValue = <T, P extends keyof T>(
items: T[],
propertyName: P,
valueToFilter: string
): T[] => {
return items.filter(item =>item[propertyName] === valueToFilter);
};
// Add a new generic type T
const filterArrayByValue = <T>(
items: T[],
propertyName: string,
valueToFilter: string
): T[] => {
return items.filter(item =>item[propertyName] === valueToFilter);
};
@frivolta
frivolta / index.tsx
Last active September 4, 2020 07:46
index.tsx - typing-react-context-v1
import * as React from "react";
import { render } from "react-dom";
import { SidebarProvider, useSidebarContext } from "./useSidebarContext";
import { ChildComponent } from "./childComponent";
export const App = () => {
const [isOpen] = useSidebarContext();
return (
<div>
@frivolta
frivolta / useSidebar.tsx
Created September 4, 2020 07:47
useSidebar.tsx - typing-react-context-v1
import { useState, useEffect } from "react";
export type UseSidebar = [
boolean,
React.Dispatch<React.SetStateAction<boolean>>
];
export const useSidebar = (newOpenValue: boolean): UseSidebar => {
const [isOpen, setIsOpen] = useState(true);
@frivolta
frivolta / useSidebarContext.tsx
Created September 4, 2020 07:48
useSidebarContext.tsx - typing-react-context-v1
import * as React from "react";
import { useSidebar, UseSidebar } from "./useSidebar";
interface Props {
children: React.ReactNode;
}
// Generate context
const SidebarContext = React.createContext<UseSidebar>(undefined!);