Skip to content

Instantly share code, notes, and snippets.

@joakin
Created August 22, 2017 15:17
Show Gist options
  • Save joakin/126823ce9a49b3838cb0169dfeb84e95 to your computer and use it in GitHub Desktop.
Save joakin/126823ce9a49b3838cb0169dfeb84e95 to your computer and use it in GitHub Desktop.
What to type
// --- 1
const app: FunctionalComponent<any> = (_props: any):JSX.Element =>
<div class="App">Hello world</div>;
export default app
// --- 2
const app: FunctionalComponent<any> = () =>
<div class="App">Hello world</div>;
export default app
// --- 3
export default (_props: any): JSX.Element =>
<div class="App">Hello world</div>;
// --- 4
export default () =>
<div class="App">Hello world</div>;
@joakin
Copy link
Author

joakin commented Aug 22, 2017

This typechecks correctly ✅

const Banana = () => <div class="App">Hello world</div>;

export default class Phone extends Component<void, void> {
  render() {
    return <Banana />
  }
}

This doesn't type check ❌

const Banana = () => <div class="App">Hello world</div>;

export default class Phone extends Component<void, void> {
  render() {
    return <Banana phone={true}/>
  }
}
const Banana: () => JSX.Element

This does type check ✅

const Banana = (props: {phone: boolean}) => <div class="App">Hello world</div>;

export default class Phone extends Component<void, void> {
  render() {
    return <Banana phone={true}/>
  }
}

This doesn't type check ❌

const Banana = (props: {phone: boolean}) => 5;

export default class Phone extends Component<void, void> {
  render() {
    return <Banana phone={true}/>
  }
}
[ts] JSX element class does not support attributes because it does not have a 'props' property.
const Banana: (props: {
    phone: boolean;
}) => number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment