-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Improve React.DOMElement type parameter for 2.4 weak type checks #17581
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
Conversation
DOMAttributes, HTMLAttributes and SVGAttributes are all weak types.
In React.DOMElement, people always pass either HTMLAttributes or
SVGAttributes, so the type parameter is just constrained to
DOMAttributes. This is not as good as a union of (HTML | SVGAttrs) for 2
reasons:
1. Valid HTMLAttributes objects that don't include some DOMAttributes
properties fail TS 2.4's weak type check.
2. Invalid HTMLAttributes objects don't get type checking for
HTMLAttributes properties.
For example:
```ts
var e: React.DOMElement<{
style: {
checked: "no"
}
}, Element>;
```
But `checked` is actually a boolean. Previously, this was not an error.
|
types/react/index.d.ts to authors (@pspeter3 @vsiao @johnnyreilly @bbenezech @pzavolinsky @digiguru @ericanderson @morcerf @tkrotoff AssureSign (account can't be detected) Microsoft (account can't be detected)). Could you review this PR? Checklist
|
|
This makes sense to me and thanks for your contribution! I'd like @vsiao to confirm since he worked on the |
|
Hi, |
|
@vsiao would you be able to take a look at this? If there's no response by the end of today then ping me and I'll merge. |
|
@johnnyreilly @vsiao I agree, the PR has been around for too long. I updated the PR and will merge it once Travis finishes successfully. |
|
@sandersn So "brute force the union" might be fine/great in this situation... but what is the "right" way to author the enum iKind {
iK1 = 1, iKN,
}
interface inBase {
kind: iKind;
}
interface i1 extends inBase {
kind: iKind.iK1;
}
interface iN extends inBase {
kind: iKind.iKN;
}
class A<T extends inBase> {
constructor(protected _a?: T) { }
}
class A1 extends A<i1> { }
class AN extends A<iN> { }
acceptsImplementationsOfBaseClass(A1);
acceptsImplementationsOfBaseClass(AN);
function acceptsImplementationsOfBaseClass(
WhichA: typeof A,
) {
return new WhichA();
}I don't want to just make it accept a union type of all |
DOMAttributes, HTMLAttributes and SVGAttributes are all weak types. In React.DOMElement, people always pass either HTMLAttributes or SVGAttributes, so the type parameter is just constrained to DOMAttributes. This is not as good as a union of (HTML | SVGAttrs) for 2 reasons:
For example:
But
checkedis actually a boolean. Previously, this was not an error.