Skip to content

Commit 83ced69

Browse files
authored
🤖 Merge PR DefinitelyTyped#69140 [react] Remove Readonly<P> from class component constructor by @eps1lon
1 parent 2949381 commit 83ced69

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

‎types/react/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ declare namespace React {
995995
*/
996996
context: unknown;
997997

998-
constructor(props: Readonly<P> | P);
998+
constructor(props: P);
999999
/**
10001000
* @deprecated
10011001
* @see {@link https://legacy.reactjs.org/docs/legacy-context.html React Docs}

‎types/react/test/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ declare const container: Element;
8282
}
8383
}
8484
class BetterPropsAndStateChecksComponent extends React.Component<Props, State, Snapshot> {
85+
constructor(props: Props) {
86+
super(props);
87+
// This should ideally error since `props` should not be mutated, but it doesn't.
88+
props.foo = 2;
89+
// @ts-expect-error
90+
this.props = { type: "foo" };
91+
}
92+
8593
render() {
8694
return null;
8795
}
@@ -117,6 +125,13 @@ declare const container: Element;
117125
};
118126
}
119127
}
128+
129+
class InferredConstructorProps extends React.Component<{ value: string }> {
130+
// @ts-expect-error ts(7006) Ideally, this would infer the props type from the type parameter but has implicit any.
131+
constructor(props) {
132+
super(props);
133+
}
134+
}
120135
}
121136

122137
class ModernComponent extends React.Component<Props, State, Snapshot>

‎types/react/ts5.0/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ declare namespace React {
996996
*/
997997
context: unknown;
998998

999-
constructor(props: Readonly<P> | P);
999+
constructor(props: P);
10001000
/**
10011001
* @deprecated
10021002
* @see {@link https://legacy.reactjs.org/docs/legacy-context.html React Docs}

‎types/react/ts5.0/test/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ declare const container: Element;
8282
}
8383
}
8484
class BetterPropsAndStateChecksComponent extends React.Component<Props, State, Snapshot> {
85+
constructor(props: Props) {
86+
super(props);
87+
// This should ideally error since `props` should not be mutated, but it doesn't.
88+
props.foo = 2;
89+
// @ts-expect-error
90+
this.props = { type: "foo" };
91+
}
92+
8593
render() {
8694
return null;
8795
}
@@ -117,6 +125,13 @@ declare const container: Element;
117125
};
118126
}
119127
}
128+
129+
class InferredConstructorProps extends React.Component<{ value: string }> {
130+
// @ts-expect-error ts(7006) Ideally, this would infer the props type from the type parameter but has implicit any.
131+
constructor(props) {
132+
super(props);
133+
}
134+
}
120135
}
121136

122137
class ModernComponent extends React.Component<Props, State, Snapshot>

0 commit comments

Comments
 (0)