-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
When an intersection is going to produce an expression too complex error... #42772
Closed
weswigham
wants to merge
1
commit into
microsoft:main
from
weswigham:reduce-frequency-of-expression-too-complex
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
//// [jsxLargeRefUnion.tsx] | ||
/// <reference path="/.lib/react16.d.ts" /> | ||
|
||
import * as React from "react"; | ||
|
||
const animated: { | ||
[Tag in keyof JSX.IntrinsicElements]: React.ForwardRefExoticComponent< | ||
React.ComponentPropsWithRef<Tag> | ||
> | ||
} = {} as any; | ||
|
||
function makeAnimated<T extends React.ElementType<any>>( | ||
comp: T | ||
): React.ForwardRefExoticComponent<React.ComponentPropsWithRef<T>> { | ||
return null as any; // not important | ||
} | ||
|
||
export interface UpgradedProps { | ||
show: boolean; | ||
} | ||
|
||
export function test<P>( | ||
component: React.ComponentType<P> | keyof React.ReactHTML | ||
): React.ComponentType<P & UpgradedProps> { | ||
// changing to `const Comp: any` un-hangs tsserver | ||
const Comp = | ||
typeof component === "string" | ||
? animated[component] | ||
: makeAnimated(component); | ||
|
||
return React.forwardRef<any, P & UpgradedProps>((props, ref) => { | ||
const { show, ...ownProps } = props; | ||
return show ? <Comp {...ownProps} ref={ref} /> : null; // ref as currently defined is expression-too-complex | ||
}); | ||
} | ||
|
||
type FixedRef<T> = string | null | React.RefObject<T> | { bivarianceHack(instance: T | null): any }["bivarianceHack"] & {current?: undefined}; | ||
declare module "react" { | ||
interface DOMElement<P extends HTMLAttributes<T> | SVGAttributes<T>, T extends Element> extends ReactElement<P> { | ||
customRef: FixedRef<T>; | ||
} | ||
} | ||
interface ForwardCustomRefRenderFunction<T, P = {}> { | ||
(props: React.PropsWithChildren<P>, ref: FixedRef<T>): React.ReactElement<any> | null; | ||
displayName?: string; | ||
defaultProps?: never; | ||
propTypes?: never; | ||
} | ||
declare function forwardCustomRef<T, P = {}>(Component: ForwardCustomRefRenderFunction<T, P>): React.ComponentType<P & React.ClassAttributes<T>>; | ||
|
||
export function test2<P>( | ||
component: React.ComponentType<P> | keyof React.ReactHTML | ||
): React.ComponentType<P & UpgradedProps> { | ||
// changing to `const Comp: any` un-hangs tsserver | ||
const Comp = | ||
typeof component === "string" | ||
? animated[component] | ||
: makeAnimated(component); | ||
|
||
return forwardCustomRef<any, P & UpgradedProps>((props, ref) => { | ||
const { show, ...ownProps } = props; | ||
return show ? <Comp {...ownProps} customRef={ref} /> : null; // with the additional `current?: undefined` member on the signature, it now can resolve | ||
}); | ||
} | ||
|
||
//// [jsxLargeRefUnion.js] | ||
"use strict"; | ||
/// <reference path="react16.d.ts" /> | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
exports.__esModule = true; | ||
exports.test2 = exports.test = void 0; | ||
var React = require("react"); | ||
var animated = {}; | ||
function makeAnimated(comp) { | ||
return null; // not important | ||
} | ||
function test(component) { | ||
// changing to `const Comp: any` un-hangs tsserver | ||
var Comp = typeof component === "string" | ||
? animated[component] | ||
: makeAnimated(component); | ||
return React.forwardRef(function (props, ref) { | ||
var show = props.show, ownProps = __rest(props, ["show"]); | ||
return show ? React.createElement(Comp, __assign({}, ownProps, { ref: ref })) : null; // ref as currently defined is expression-too-complex | ||
}); | ||
} | ||
exports.test = test; | ||
function test2(component) { | ||
// changing to `const Comp: any` un-hangs tsserver | ||
var Comp = typeof component === "string" | ||
? animated[component] | ||
: makeAnimated(component); | ||
return forwardCustomRef(function (props, ref) { | ||
var show = props.show, ownProps = __rest(props, ["show"]); | ||
return show ? React.createElement(Comp, __assign({}, ownProps, { customRef: ref })) : null; // with the additional `current?: undefined` member on the signature, it now can resolve | ||
}); | ||
} | ||
exports.test2 = test2; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RE: This comment and associated logic. If we did this in all scenarios, it would be a breaking change; however since we only do it when we would have previously issued a
Expression produces a union type that is too complex to represent
error, it is not. I think we've had a desire to try to eliminate structurally branded primitives from the type system for awhile, and I think this is just one small corner where we can actually remove them and be pretty safe, without worrying about either breaking people or needing to provide an alternative.