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
type SomeObjectType; | |
type AnotherObjectType; | |
type AnyOtherObjectYouWantToAddType; | |
const isPresentObjectGeneric = <T>(arg: T): boolean => { | |
if (arg && Object.keys(arg).length > 0) { | |
return true; | |
} | |
return false; | |
} |
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
const identity = <T>(arg: T): T => arg; | |
identity<string>('someString'); //would return 'someString' | |
identity<number>(12); //would return 12 | |
identity<boolean>(false); //would return false |
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
const identityStringFn = (arg: string): string => arg; | |
const identityNumberFn = (arg: number): number => arg; | |
const identityBooleanFn = (arg: boolean): boolean => arg; |
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
//Keep adding more types to the union, good luck with that | |
type ObjectType = SomeObjectType | AnotherObjectType | |
| ... | AnyOtherObjectYouWantToAddType | undefined; | |
//OR | |
//Just make the argument of type any 😱 | |
const isPresentAny = (arg: any): boolean => { | |
if (arg && Object.keys(arg).length > 0) { | |
return true; |
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
type ObjectType = SomeObjectType | AnotherObjectType | undefined; | |
const isPresentSomeAnotherObject = (arg: ObjectType): boolean => { | |
if (arg && Object.keys(arg).length > 0) { | |
return true; | |
} | |
return false; | |
} |
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
if (isPresentSomeObject(someObject)) { | |
//Back off compiler, I got this! | |
someObject!.id = 12; | |
} |
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
const isPresentSomeObject = (arg: SomeObjectType | undefined): boolean => { | |
if (arg && Object.keys(arg).length > 0) { | |
return true; | |
} | |
return false; | |
} |
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
if (someObject && Object.keys(someObject).length > 0) { | |
//Our updated test to check if an object is really not empty | |
} |
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
type SomeObjectType = { | |
id: number; | |
text: string; | |
} | |
let someObject: SomeObjectType | undefined; | |
if (someObject && Object.keys(someObject).length > 0) { | |
//This evaluates as truthy because it's not undefined | |
someObject.id = 12; //TS compiler doesn't complain here because the object exists | |
} |
NewerOlder