CSSStyleDeclaration string properties all have `| null` type declaration.
See original GitHub issuelib.dom.d.ts has recently suffixed all of the properties of CSSStyleDeclaration with | null:
https://github.com/Microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L1382
https://github.com/Microsoft/TypeScript/commit/6814c1d883791a1ad976f3146315de30e00dc0b9
For those of us using --strictNullChecks, this is forcing a null assertion (!) every time a property is used, even though according to the spec null will never be returned, only an empty string:
https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue
The camel-cased attribute attribute, on getting, must return the result of invoking getPropertyValue() with the argument being the result of running the IDL attribute to CSS property algorithm for camel-cased attribute.
The spec does seem to allow null as an argument for setPropertyValue, so technically this a case where allowing a different type for a getter and setter (#2521) would be required to accurately model a builtin.
But in the absence of that, this particular object treats the empty string the same as null for setting, so I think it would be cleaner to simply remove | null from this definition so that we don’t have to needlessly null-assert everywhere CSSStyleDeclaration is used.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:7 (4 by maintainers)

Top Related StackOverflow Question
Currently we have mixed
stringandstring | nulltypings 8-/ https://github.com/Microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L2749Looks like this has landed in TS 3.6.4