diff --git a/types/react/index.d.ts b/types/react/index.d.ts index 3b18b88bcd717c..d3eb686c96bed5 100644 --- a/types/react/index.d.ts +++ b/types/react/index.d.ts @@ -995,7 +995,7 @@ declare namespace React { */ context: unknown; - constructor(props: Readonly

| P); + constructor(props: P); /** * @deprecated * @see {@link https://legacy.reactjs.org/docs/legacy-context.html React Docs} diff --git a/types/react/test/index.ts b/types/react/test/index.ts index 7f6e9267cb4b12..8df8b6c62f24bf 100644 --- a/types/react/test/index.ts +++ b/types/react/test/index.ts @@ -82,6 +82,14 @@ declare const container: Element; } } class BetterPropsAndStateChecksComponent extends React.Component { + constructor(props: Props) { + super(props); + // This should ideally error since `props` should not be mutated, but it doesn't. + props.foo = 2; + // @ts-expect-error + this.props = { type: "foo" }; + } + render() { return null; } @@ -117,6 +125,13 @@ declare const container: Element; }; } } + + class InferredConstructorProps extends React.Component<{ value: string }> { + // @ts-expect-error ts(7006) Ideally, this would infer the props type from the type parameter but has implicit any. + constructor(props) { + super(props); + } + } } class ModernComponent extends React.Component diff --git a/types/react/ts5.0/index.d.ts b/types/react/ts5.0/index.d.ts index 6cf9a3c1c5e568..93d721f6c2be59 100644 --- a/types/react/ts5.0/index.d.ts +++ b/types/react/ts5.0/index.d.ts @@ -996,7 +996,7 @@ declare namespace React { */ context: unknown; - constructor(props: Readonly

| P); + constructor(props: P); /** * @deprecated * @see {@link https://legacy.reactjs.org/docs/legacy-context.html React Docs} diff --git a/types/react/ts5.0/test/index.ts b/types/react/ts5.0/test/index.ts index dbb1a7f7ba70b8..2bd8cc97c427b4 100644 --- a/types/react/ts5.0/test/index.ts +++ b/types/react/ts5.0/test/index.ts @@ -82,6 +82,14 @@ declare const container: Element; } } class BetterPropsAndStateChecksComponent extends React.Component { + constructor(props: Props) { + super(props); + // This should ideally error since `props` should not be mutated, but it doesn't. + props.foo = 2; + // @ts-expect-error + this.props = { type: "foo" }; + } + render() { return null; } @@ -117,6 +125,13 @@ declare const container: Element; }; } } + + class InferredConstructorProps extends React.Component<{ value: string }> { + // @ts-expect-error ts(7006) Ideally, this would infer the props type from the type parameter but has implicit any. + constructor(props) { + super(props); + } + } } class ModernComponent extends React.Component