Skip to content

Instantly share code, notes, and snippets.

Filippo Rivolta frivolta

Block or report user

Report or block frivolta

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View genericType.ts
// Add a new generic type T
const filterArrayByValue = <T>(
items: T[],
propertyName: string,
valueToFilter: string
): T[] => {
return items.filter(item =>item[propertyName] === valueToFilter);
};
View extendGenericType.ts
// 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);
};
View finalCode.ts
type Book = {
title: string;
genre: string;
publicationYear: number;
};
type Car = {
modelName: string;
type: string;
price: number;
View finalFunction.ts
// 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);
};
View initialFunction.ts
// 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},
View initialFunction.ts
// Define a book type
type Book = {
title: string;
genre: string;
publicationYear: number;
};
// Define a list of types
const listOfBooks: Book[] = [
{
@frivolta
frivolta / tsconfig.json
Created Jan 8, 2020
TsConfig Boilerplate
View tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"removeComments": true,
"noImplicitAny": true,
You can’t perform that action at this time.