Last active
July 28, 2021 11:49
-
-
Save neilgaietto/42ead318c8f0d43fb916960807ed4d05 to your computer and use it in GitHub Desktop.
Testing combining typescript interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// base | |
export interface MyThing { | |
name: string; | |
} | |
// first type of calcs for base | |
export interface MyThingCalcA { | |
calcA: number; | |
} | |
// second type of calcs for base | |
export interface MyThingCalcB { | |
calcB: number; | |
} | |
// used to extend an object with calcs | |
export interface CalcThing<T> { | |
calculated: T; | |
} | |
// My Thing with various calculations attached | |
export interface MyThingCalculated<T extends MyThingCalcA | MyThingCalcB> extends MyThing, CalcThing<T>{ | |
} | |
// option 1a: Insert object, return calcs only | |
export function useThingInAndCalcOnlyOut(thing: MyThing): MyThingCalcA { | |
return { calcA: 123 }; | |
} | |
// option 1b: Multi calcs returned | |
export function useThingInAndMultiCalcOut(thing: MyThing): MyThingCalcA & MyThingCalcB { | |
return { calcA: 123, calcB: 321 }; | |
} | |
// option 2a: Insert object, return object with calcs attached | |
export function useThingInAndFullCalcOut(thing: MyThing): MyThingCalculated<MyThingCalcB> { | |
return { ...thing, calculated: {calcB: 123} }; | |
} | |
// option 2b: Insert object, return object with multiple calcs attached | |
export function useThingInAndFullMultiCalcOut(thing: MyThing): MyThingCalculated<MyThingCalcA & MyThingCalcB> { | |
return { ...thing, calculated: {calcA: 123, calcB: 321} }; | |
} | |
// -- Testing | |
// const x = useThingInAndFullMultiCalcOut({name: 'test'}); | |
// x.name; | |
// x.calculated.calcA; | |
// x.calculated.calcB; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment