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
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
interface Cat { | |
meow(); | |
walk(); | |
} | |
interface Dog { | |
bark(); | |
walk(); | |
} |
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 isCat = (animal: Animal):boolean => { | |
return (animal as Cat).meow | |
} | |
const isDog = (animal: Animal): boolean => { | |
return (animal as Dog).bark | |
} |
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 isCat = (animal: Animal): animal is Cat => { | |
//Hey compiler, this animal is a Cat | |
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 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 | |
} |
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
//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 SomeObjectType; | |
type AnotherObjectType; | |
type AnyOtherObjectYouWantToAddType; | |
const isPresentObjectGeneric = <T>(arg: T): boolean => { | |
if (arg && Object.keys(arg).length > 0) { | |
return true; | |
} | |
return false; | |
} |
OlderNewer