Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[react] Add support for legacy context in JSXElementConstructor #65566

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion types/react/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ declare namespace React {
type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;

type JSXElementConstructor<P> =
| ((props: P) => ReactElement<any, any> | null)
| ((
props: P,
/**
* @deprecated https://legacy.reactjs.org/docs/legacy-context.html#referencing-context-in-stateless-function-components
*/
deprecatedLegacyContext?: any,
) => ReactElement<any, any> | null)
| (new (props: P) => Component<any, any>);

interface RefObject<T> {
Expand Down
6 changes: 6 additions & 0 deletions types/react/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,12 @@ const propsWithoutRef: React.PropsWithoutRef<UnionProps> = {
Wrapper = (props: ExactProps) => null;
Wrapper = class Wider extends React.Component<WiderProps> {};
Wrapper = (props: WiderProps) => null;
Wrapper = (props, legacyContext) => {
// $ExpectType any
legacyContext;
return null;
};
Wrapper = (props, legacyContext: { foo: number }) => null;

React.createElement(Wrapper, { value: 'A' });
React.createElement(Wrapper, { value: 'B' });
Expand Down
12 changes: 12 additions & 0 deletions types/react/test/tsx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,15 @@ function elementTypeTests() {
}
}

const ReturnWithLegacyContext = (props: { foo: string }, context: { bar: number }) => {
return (
<div>
foo: {props.foo}, bar: {context.bar}
</div>
);
};
const FCWithLegacyContext: React.FC<{ foo: string }> = ReturnWithLegacyContext;

// Desired behavior.
// @ts-expect-error
<ReturnVoid />;
Expand Down Expand Up @@ -764,6 +773,9 @@ function elementTypeTests() {
<RenderPromise />;
// Will not type-check in a real project but accepted in DT tests since experimental.d.ts is part of compilation.
React.createElement(RenderPromise);

<ReturnWithLegacyContext foo="one" />;
React.createElement(ReturnWithLegacyContext, {foo: 'one'});
}

function managingRefs() {
Expand Down
8 changes: 7 additions & 1 deletion types/react/ts5.0/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ declare namespace React {
type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;

type JSXElementConstructor<P> =
| ((props: P) => ReactElement<any, any> | null)
| ((
props: P,
/**
* @deprecated https://legacy.react/ts5.0js.org/docs/legacy-context.html#referencing-context-in-stateless-function-components
*/
deprecatedLegacyContext?: any,
) => ReactElement<any, any> | null)
| (new (props: P) => Component<any, any>);

interface RefObject<T> {
Expand Down
6 changes: 6 additions & 0 deletions types/react/ts5.0/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,12 @@ const propsWithoutRef: React.PropsWithoutRef<UnionProps> = {
Wrapper = (props: ExactProps) => null;
Wrapper = class Wider extends React.Component<WiderProps> {};
Wrapper = (props: WiderProps) => null;
Wrapper = (props, legacyContext) => {
// $ExpectType any
legacyContext;
return null;
};
Wrapper = (props, legacyContext: { foo: number }) => null;

React.createElement(Wrapper, { value: 'A' });
React.createElement(Wrapper, { value: 'B' });
Expand Down
12 changes: 12 additions & 0 deletions types/react/ts5.0/test/tsx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,15 @@ function elementTypeTests() {
}
}

const ReturnWithLegacyContext = (props: { foo: string }, context: { bar: number }) => {
return (
<div>
foo: {props.foo}, bar: {context.bar}
</div>
);
};
const FCWithLegacyContext: React.FC<{ foo: string }> = ReturnWithLegacyContext;

// Desired behavior.
// @ts-expect-error
<ReturnVoid />;
Expand Down Expand Up @@ -764,6 +773,9 @@ function elementTypeTests() {
<RenderPromise />;
// Will not type-check in a real project but accepted in DT tests since experimental.d.ts is part of compilation.
React.createElement(RenderPromise);

<ReturnWithLegacyContext foo="one" />;
React.createElement(ReturnWithLegacyContext, {foo: 'one'});
}

function managingRefs() {
Expand Down