Skip to content

Commit 4f2ef53

Browse files
committed
fixed inferred type of component with default props -> when nullable property is provided a default it should be asserted as not nullable for component interface
resolved piotrwitek#43
1 parent 4fd21fd commit 4f2ef53

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

playground/src/components/stateful-counter-with-default.tsx

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,40 @@ interface State {
1313
count: number;
1414
}
1515

16-
export class StatefulCounterWithDefault extends React.Component<StatefulCounterWithDefaultProps, State> {
17-
// to make defaultProps strictly typed we need to explicitly declare their type
18-
// @see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11640
19-
static defaultProps: DefaultProps = {
20-
initialCount: 0,
21-
};
22-
23-
props: StatefulCounterWithDefaultProps & DefaultProps;
16+
export const StatefulCounterWithDefault: React.ComponentClass<StatefulCounterWithDefaultProps> =
17+
class extends React.Component<StatefulCounterWithDefaultProps & DefaultProps> {
18+
// to make defaultProps strictly typed we need to explicitly declare their type
19+
// @see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11640
20+
static defaultProps: DefaultProps = {
21+
initialCount: 0,
22+
};
23+
24+
state: State = {
25+
count: this.props.initialCount,
26+
};
27+
28+
componentWillReceiveProps({ initialCount }: StatefulCounterWithDefaultProps) {
29+
if (initialCount != null && initialCount !== this.props.initialCount) {
30+
this.setState({ count: initialCount });
31+
}
32+
}
2433

25-
state: State = {
26-
count: this.props.initialCount,
27-
};
34+
handleIncrement = () => {
35+
this.setState({ count: this.state.count + 1 });
36+
}
2837

29-
componentWillReceiveProps({ initialCount }: StatefulCounterWithDefaultProps) {
30-
if (initialCount != null && initialCount !== this.props.initialCount) {
31-
this.setState({ count: initialCount });
38+
render() {
39+
const { handleIncrement } = this;
40+
const { label } = this.props;
41+
const { count } = this.state;
42+
43+
return (
44+
<div>
45+
<span>{label}: {count} </span>
46+
<button type="button" onClick={handleIncrement}>
47+
{`Increment`}
48+
</button>
49+
</div>
50+
);
3251
}
33-
}
34-
35-
handleIncrement = () => {
36-
this.setState({ count: this.state.count + 1 });
37-
}
38-
39-
render() {
40-
const { handleIncrement } = this;
41-
const { label } = this.props;
42-
const { count } = this.state;
43-
44-
return (
45-
<div>
46-
<span>{label}: {count} </span>
47-
<button type="button" onClick={handleIncrement}>
48-
{`Increment`}
49-
</button>
50-
</div>
51-
);
52-
}
53-
}
52+
};

playground/src/components/stateful-counter-with-default.usage.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ import { StatefulCounterWithDefault } from '@src/components';
55
export default () => (
66
<StatefulCounterWithDefault
77
label={'StatefulCounter'}
8-
initialCount={10}
98
/>
109
);

0 commit comments

Comments
 (0)