From 12df4a550bc430c489f8181ab3bebc8dd1153d2e Mon Sep 17 00:00:00 2001 From: eps1lon Date: Sat, 23 Mar 2024 18:13:11 +0100 Subject: [PATCH 1/2] [react] Add test highlighting the `Readonly` in the constructor signature does not do anything --- types/react/test/index.ts | 15 +++++++++++++++ types/react/ts5.0/test/index.ts | 15 +++++++++++++++ 2 files changed, 30 insertions(+) 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/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 From d6ea30e3cdf3c26f9cd15c702d225e1a34169e41 Mon Sep 17 00:00:00 2001 From: eps1lon Date: Sat, 23 Mar 2024 18:19:20 +0100 Subject: [PATCH 2/2] [react] Remove Readonly

from class component constructor This was added in https://github.com/DefinitelyTyped/DefinitelyTyped/commit/542f3c08a5eb3db0507a1d03272fa0c2bca9f551 without adding tests validating the behavior. It certainly doesn't work anymore since the added tests also passed with `Readonly

`. Maybe it did work but then the change will almost certainly be marked as working as intended. So I just remove it now since a lot of the ecosystem have inlined their own class component constructor without Readonly

that matches the legacy context constructur signature. Legacy context will be removed in React 19 so we would break a lot of existing code for a feature that doesn't even work. --- types/react/index.d.ts | 2 +- types/react/ts5.0/index.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/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}