1

The prop-types of list is PTs.arrayOf(PTs.string), is incompatible with TypeScript type. The list props can be empty array.

import React from 'react';
import PTs from 'prop-types';

interface AppProps {
  list?: string[];
}
const App: React.FC<AppProps> = ({ list }) => {
  return <div>app</div>
}

App.displayName = 'App';
App.defaultProps = {
  list: []
};
App.propTypes = {
  list: PTs.arrayOf(PTs.string) //<- error here
};

Got TS type error:

Type 'Requireable<(string | null | undefined)[]>' is not assignable to type 'Validator<string[] | null | undefined>'.
  Types of property '[nominalTypeHack]' are incompatible.
    Type '{ type: (string | null | undefined)[] | null | undefined; } | undefined' is not assignable to type '{ type: string[] | null | undefined; } | undefined'.
      Type '{ type: (string | null | undefined)[] | null | undefined; }' is not assignable to type '{ type: string[] | null | undefined; }'.
        Types of property 'type' are incompatible.
          Type '(string | null | undefined)[] | null | undefined' is not assignable to type 'string[] | null | undefined'.
            Type '(string | null | undefined)[]' is not assignable to type 'string[]'.
              Type 'string | null | undefined' is not assignable to type 'string'.
                Type 'undefined' is not assignable to type 'string'.(2322)

TypeScript Playground

1 Answer 1

1

You should change the definition to:

App.propTypes = {
  list: PTs.arrayOf(PTs.string.isRequired)
};

TS Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.