This file contains hidden or 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
// Perfectly valid because in JavaScript you can access characters in a string by index | |
// example: | |
// const x = 'coffee' | |
// const letterF = x[3] // result: 'f' | |
const result = something('hello', 50) | |
// NOT valid because in JavaScript you can't access characters in a string by a string | |
const result = something('hello', '50') |
This file contains hidden or 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 result = something({ goodwill: 'a' }, 'toString') |
This file contains hidden or 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
// Completely valid -- TypeScript will not complain | |
const result = something({ goodwill: 'a' }, 'goodwill') |
This file contains hidden or 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
function something<T, K extends keyof T>(arg1: T, arg2: K) { | |
// | |
} |
This file contains hidden or 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
function combine<T, W>(arg1: W, arg2: T): T { | |
return arg2 | |
} |
This file contains hidden or 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
function combine(arg1: string, arg2: string): string { | |
return arg2 | |
} |
This file contains hidden or 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
function combine<T, W>(arg1: W, arg2: T): T { | |
return arg2 | |
} |
This file contains hidden or 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
let fruit = 'apple' | |
function sayStuff(stuff) { | |
window.alert(stuff) | |
} | |
sayStuff(`i love eating ${fruit}s`) // implicit | |
// or | |
sayStuff < number > 500 // explicit |
This file contains hidden or 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
function logAndReturnIt<T>( | |
valueThatIsGoingToBeLoggedAndReturnedFromAUselessFunction: T, | |
): T { | |
console.log(typeof valueThatIsGoingToBeLoggedAndReturnedFromAUselessFunction) | |
return valueThatIsGoingToBeLoggedAndReturnedFromAUselessFunction | |
} |
This file contains hidden or 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
function logAndReturnIt( | |
valueThatIsGoingToBeLoggedAndReturnedFromAUselessFunction: any, | |
): any { | |
console.log(typeof valueThatIsGoingToBeLoggedAndReturnedFromAUselessFunction) | |
return valueThatIsGoingToBeLoggedAndReturnedFromAUselessFunction | |
} | |
logAndReturnIt(null) // valid | |
logAndReturnIt(5) // valid |