Incorrect TypeScript definition for Menu Component defaultProps
See original GitHub issueVersion
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:
- Created 6 years ago
- Comments:14 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:
Then the extracted
.d.ts
files should infer the correct type (ButtonType
instead ofstring
).Will see if I can find some time to submit a PR