microsoft / microsoft/TypeScript
React and defaultProps cant pass correct props to inner function
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 2.4.0
Code
import * as React from 'react';
interface CircleProps {
percent?: number
}
export default class Circle extends React.Component<CircleProps> {
public static defaultProps: Partial<CircleProps> = {
percent: 100
}
calculateDashArray(percent: number): string {
return `${percent}`
}
render() {
const strokeDasharray = this.calculateDashArray(this.props.percent)
return (
<div>{strokeDasharray}</div>
)
}
}
Expected behavior:
this.calculateDashArray should correct accept value of defalutProps
Actual behavior:
Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
Type 'undefined' is not assignable to type 'number'.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the TypeScript 2.4 example in the issue and inspect how React.Component props and static defaultProps are represented. Determine the intended type of this.props.percent inside render, then add or update compiler tests for the shown error and default-prop behavior. Done means the example accepts the defaulted value without weakening unrelated prop checking.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- compilers, frontend
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100