wangduanduan / wangduanduan.github.io

Wubba Lubba dub-dub
https://wdd.js.org
27 stars 7 forks source link

Property 'name' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'. TS2339 #306

Closed wangduanduan closed 5 years ago

wangduanduan commented 5 years ago

Property 'name' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'. TS2339

You forgot to declare name as a React property in the ProfileData class definition. Something like this should work:

class ProfileData extends React.Component<{name: string}, person> {
wangduanduan commented 5 years ago

By default, all React.Components have a this.props type of {}, which we saw in the & statement in the error. Since we really do want to have a fieldName prop, we have to add it:

interface Props {
  fieldName: string;
}
interface State {
  error: string | null;
}
class CustomInput extends React.Component<Props, State> {
  state = {
    error: null;
  };
}