question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect TypeScript definition for Menu Component defaultProps

See original GitHub issue

Version

3.1.0

Environment

Chrome

Reproduction link

https://codesandbox.io/s/o7lxqxy7xz

Steps to reproduce

Version: 3.1.0

Menu Component’s defaultProps is defined as:

static defaultProps: {
    theme: string;
    ...
};

But MenuProps interface is expecting:

theme?: 'light' | 'dark';

What is expected?

No error

What is actually happening?

This is causing TypeScript compiler to throw:

Error:(87, 25) TS2345: Argument of type 'typeof Menu' is not assignable to parameter of type 'string | ComponentClass<MenuProps> | StatelessComponent<MenuProps>'.
  Type 'typeof Menu' is not assignable to type 'StatelessComponent<MenuProps>'.
    Types of property 'defaultProps' are incompatible.
      Type '{ prefixCls: string; className: string; theme: string; }' is not assignable to type 'Partial<MenuProps>'.
        Types of property 'theme' are incompatible.
          Type 'string' is not assignable to type '"light" | "dark"'.

This can be corrected by changing defaultProps to:

static defaultProps: {
    theme: 'light' | 'dark';
    ...
};

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jmcacommented, Jan 2, 2018

@dengfuping I’m thinking it’s an issue with TS Inference. Here’s a quick page on it: https://basarat.gitbooks.io/typescript/docs/types/literal-types.html

Referring to https://github.com/ant-design/ant-design/blob/c14b13d4acbf0c51f0ef67a93270516ac0572a0a/components/modal/Modal.tsx#L98 TS is inferring that this prop should be a string type.

The solution could possibly be to just cast the prop:

static defaultProps = {
    ...
    okType: 'primary' as ButtonType,
};

Then the extracted .d.ts files should infer the correct type (ButtonType instead of string).

0reactions
eddiemoorecommented, Apr 11, 2018

Will see if I can find some time to submit a PR

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typing defaultProps - React TypeScript Cheatsheets
A component with defaultProps may seem to have some required props that actually aren't. Problem Statement​. Here's what you want to do: interface...
Read more >
Typescript/React extending default functional component ...
In this case you can define a DefaultProps interface and then use it to extend any Component Props: interface DefaultProps { onClick?:
Read more >
Eslint: Problem with default props in functional component ...
defaultProp is used when the passed prop is null or undefined interface Props { name: string; gretting?: string;// question mark means it could...
Read more >
React, TypeScript and defaultProps dilemma | by Martin Hochel
In this article I will demonstrate the issue and how to solve it via class Components. So let's define a Button component, with...
Read more >
Props | Vue.js
Vue components require explicit props declaration so that Vue knows what ... using your component in the browser console if they pass the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found