Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@sandersn
Copy link
Contributor

@sandersn sandersn commented Jun 28, 2017

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:

var e: React.DOMElement<{
  style: {
    checked: "no"
  }
}, Element>;

But checked is actually a boolean. Previously, this was not an error.

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.
@dt-bot
Copy link
Member

dt-bot commented Jun 28, 2017

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?
👍 or 👎?

Checklist

  • pass the Travis CI test?

@pspeter3
Copy link
Contributor

This makes sense to me and thanks for your contribution! I'd like @vsiao to confirm since he worked on the DOMAttributes originally.

@izikorgad
Copy link

Hi,
First of all, thanks for all your efforts.
We are also waiting for this fix - do you guys have a ETA for t=the next release?

@johnnyreilly
Copy link
Member

@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.

@sandersn
Copy link
Contributor Author

sandersn commented Jul 6, 2017

@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 sandersn merged commit 85b8790 into master Jul 6, 2017
@SlurpTheo
Copy link

SlurpTheo commented Jul 13, 2017

@sandersn So "brute force the union" might be fine/great in this situation... but what is the "right" way to author the acceptsImplementationsOfBaseClass function below given a ^2.4.1 world?

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 A1 | ... | AN (imagine it's a very large, constantly growing list).

@RyanCavanaugh RyanCavanaugh deleted the improve-react-domelement-type-parameter branch August 31, 2018 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants