From 0d61b7167858854f0e744b66d40bed321952fc78 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 28 Jun 2017 09:39:36 -0700 Subject: [PATCH] Improve React.DOMElement type parameter for 2.4 weak type checks 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 | 2 +- types/react/test/index.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/types/react/index.d.ts b/types/react/index.d.ts index 577959ae7b..c03a240c8b 100644 --- a/types/react/index.d.ts +++ b/types/react/index.d.ts @@ -66,7 +66,7 @@ declare namespace React { type ClassicElement

= CElement>; - interface DOMElement

, T extends Element> extends ReactElement

{ + interface DOMElement

| SVGAttributes, T extends Element> extends ReactElement

{ type: string; ref: Ref; } diff --git a/types/react/test/index.ts b/types/react/test/index.ts index 27acc07d34..21a8f45e33 100644 --- a/types/react/test/index.ts +++ b/types/react/test/index.ts @@ -715,3 +715,13 @@ const formEvent:InputFormEvent = changeEvent; } const VariableWithAClass: React.ComponentClass = ComponentWithDefaultProps; } + +// complex React.DOMElement type +declare var x: React.DOMElement<{ + className: string; + style: { + height: string; + overflowY: "auto"; + transition: string; + }; +}, Element>;