Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typed react - props as `type` or an `interface`

I have react code

export default class MyComponent extends Component<Props,State>

The question is, do I write props like a type or an interface?

type Props = {     isActive: Boolean,     onClick: Function } 

or

interface Props {     isActive: Boolean,     onClick: Function } 

also, what is the direrence, when I am not using typescript, but classic webpack+babel setup?

Or, does it even matter much to mee?

like image 530
Ivan Hanák Avatar asked Mar 29 '18 18:03

Ivan Hanák


People also ask

Should you use type or interface for React props?

And usually there will be someone who's already dabbled in TypeScript who asks me why I choose to use an interface instead of a type alias for defining the props. The short answer is that interfaces and type alias are effectively interchangeable for defining objects in TypeScript.

What is the type of props React?

Props are the read-only properties that are shared between components to give the unidirectional flow of React a dynamic touch. They're mainly shared from the parent component to the child component, but the reverse is also possible (though not recommended).

Should I use type or interface TypeScript?

Interfaces are most recommended for defining new objects or methods or properties of an object where it will receive a specific component. Hence interface works better when using objects and method objects. Therefore it is our choice to choose between types or interface according to the program needs.

How do you assign a prop to type in React?

To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends React. Component { render() { return ( <h1>Hello, {this.props.name}</h1> ); } } Greeting. propTypes = { name: PropTypes.


Video Answer


1 Answers

It is 2020 now and I would favor type in almost all cases with React props (general type vs interface post is here). Common cases that can be only expressed with type aliases:

// given some props from another comp that are to be altered type ExternalProps = { a: string; b: { c: number } };  type Props_IndexType = ExternalProps["b"]; // { c: number; } type Props_MappedType = { [K in keyof ExternalProps]: number }; // { a: number; b: number; } type Props_DiscriminatedUnionType = { tag: "tag1"; foo: string } | { tag: "tag2"; foo: boolean} type Props_typeOf = { foo: string } & typeof defaultProps; // see class comp example  // conditional types - ok, this one is a bit contrived, but you get the point type Props_ConditionalType<T> = T extends true ? { a: string } : { b: number }; const Comp = <T extends {}>(props: Props_ConditionalType<T>) =>   <div>{"a" in props && (props as any).a}</div> render(<Comp<true> a="foo" />, document.getElementById("root")); 

Class component example for illustration (OP mentions them, but above cases also apply for Hooks):

// cannot do that with interfaces type Props = ({ tag: "tag1"; foo: string } | { tag: "tag2"; foo: boolean }) &   typeof defaultProps; type State = typeof initState;  const defaultProps = { a: "A" }; const initState = { c: "C" };  class App extends React.Component<Props, State> {   static readonly defaultProps = defaultProps;   state = initState;    render() { ... } }  render(<App tag="tag1" foo="foo" />, document.getElementById("root")); 

The only cases, I would consider interfaces:

  • You do declaration merging of prop types in the global scope (uncommon nowadays)
  • You want to hide type implementation details, as interfaces create a new name used in error messaged, IDE type infos etc. (docs)
like image 160
ford04 Avatar answered Sep 27 '22 20:09

ford04