Last active
August 8, 2021 17:58
-
-
Save Ebazhanov/4c0e0b8270adf5fc95d6ead459d034d6 to your computer and use it in GitHub Desktop.
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
/** | |
* error: "Binding element 'name' implicitly has an 'any' type.ts(7031)" | |
**/ | |
// BEFORE | |
export function Heading ({name}){ | |
return <h1>Your {name}</h1> | |
} | |
// variant #1 | |
export function Heading ({name}: {name: string}){ | |
return <h1>Your {name}</h1> | |
} | |
// variant #2 "extracting to types" | |
type HeadingProps = {name: string}; | |
export function Heading ({name}: HeadingProps){ | |
return <h1>Your {name}</h1> | |
} | |
// variant #3 "with default value" | |
type HeadingProps = {name: string}; | |
export function Heading ({name = 'Zhenja'}: HeadingProps){ | |
return <h1>Your {name}</h1> | |
} | |
// variant #4 "with ES6 make the name optional with `?` question mark" | |
type HeadingProps = {name?: string}; | |
export function Heading ({name = 'Zhenja'}: HeadingProps){ | |
return <h1>Your {name}</h1> | |
} | |
/** | |
* we are using `type` defeniction for props and for state in react components | |
* and `interfeces` for public API | |
**/ | |
interface HeadingProps { | |
name: string; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment