Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import { get } from "lodash-es";
interface Person {
name: string;
age: number;
children?: Person[];
}
declare type DriverType = "Race" | "Commercial" | "Personal";
interface Driver extends Person {
type: DriverType;
car: Car;
}
interface Car {
make: string;
model: string;
year: number;
}
interface ToyCar {
brand: string;
}
interface NumericCarDict<T> {
[key: number]: T;
}
const person: Person = {
name: "Bob",
age: 41
};
const civic: Car = {
make: "Honda",
model: "Civic",
year: 2001
};
const forester: Car = {
make: 'Subaru',
model: 'Forester',
year: 2015
};
const bobDriver: Driver = {
...person,
type: "Personal",
car: civic
};
const hotWheel = {
brand: 'Hotwheels'
};
const carDict: NumericCarDict<Car> = {
0: civic,
1: forester
};
const anyObj: any = {
really: 'yes'
};
let undefinedVar: undefined;
let nullVar: null = null;
/*
get<TObject extends object, TKey extends keyof TObject>(
object: TObject,
path: TKey | [TKey]
): TObject[TKey];
*/
const tObjProp: Car = get<Driver, "car">(bobDriver, "car");
/**
get<TObject extends object, TKey extends keyof TObject>(
object: TObject | null | undefined,
path: TKey | [TKey]
): TObject[TKey] | undefined;
*/
const tObjOrNull: Car | undefined = get<Driver, "car">(bobDriver, "car");
/**
get<TObject extends object, TKey extends keyof TObject, TDefault>(
object: TObject | null | undefined,
path: TKey | [TKey],
defaultValue: TDefault
): TObject[TKey] | TDefault;
*/
const withDefault: Car = get<Driver, "car", Car>(bobDriver, "car", {
make: "Ford",
model: "Mustang",
year: 2018
});
const defaultOtherType: Car | ToyCar = get<Driver, "car", ToyCar >(bobDriver, "car", hotWheel);
/**
get<T>(
object: NumericDictionary<T>,
path: number
): T;
*/
const dictGet: Car = get<Car>(carDict, 0);
/**
get<T>(
object: NumericDictionary<T> | null | undefined,
path: number
): T | undefined;
*/
const dictGetNull: Car | undefined = get<Car>(carDict, 4);
const dictGetNull2: Car | undefined = get<Car>(nullVar, 4);
const dictGetNull3: Car | undefined = get<Car>(undefinedVar, 4);
/**
get<T>(
object: NumericDictionary<T> | null | undefined,
path: number,
defaultValue: TDefault
): T | TDefault;
*/
const dictGetWithDefault: Car | ToyCar = get<Car, ToyCar>(carDict, 4, hotWheel);
const dictGetWithDefault2: Car | ToyCar = get<Car, ToyCar>(nullVar, 4, hotWheel);
const dictGetWithDefault3: Car | ToyCar = get<Car, ToyCar>(undefinedVar, 4, hotWheel);
/*
get<TDefault>(
object: null | undefined,
path: PropertyPath,
defaultValue: TDefault
): TDefault;
*/
const alwaysDefaultNull: ToyCar = get<ToyCar>(nullVar, 'brand', hotWheel);
const alwaysDefaultUndefined: ToyCar = get<ToyCar>(undefinedVar, 'brand', hotWheel);
/**
get(
object: null | undefined,
path: PropertyPath
): undefined;
*/
const alwaysUndefinedNull: undefined = get(nullVar,'property.path.to.thing');
const alwaysUndefined: undefined = get(undefinedVar,'property.path.to.thing');
/**
get(
object: any,
path: PropertyPath,
defaultValue?: any
): any;
*/
const getAny: any = get(anyObj, 'really', 104); // nothing matters anymore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.