You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A lot of teams have seen production bugs from trying to use code not available in older browsers. performance.now, for example, is listed in lib.d.ts but shouldn't be used on sites that support IE10 and don't polyfill performance.
Two other ways of getting around this class of problem without dropping browsers or adding polyfills (are there other good ones?) are:
Manually banning scores of built-in types with TSLint
Dropping "dom" from "lib" in tsconfig.json
Removing "dom" typings works great to enforce using only non-browser APIs (or dependency-injecting them where used), which is much better than calling APIs like performance.now and setTimeout willy nilly. However, you now need to manually re-write typings for all these native things as you go.
The solution would be much simpler if TypeScript exposed a form of lib.d.ts that included only the interfaces and types for all these constructs without the declare var/declare function.
Using setTimeout as an example, it's declared without a separate ISetTimeout interface in TypeScript's lib.d.ts:
// Only in the standard ("loose") version:declarevarsetTimeout: SetTimeout;
Errata: I personally remove "dom" whenever possible.. but a lot of @types definitions assume it exists and it's a bit like whack-a-mole trying to fix typings on DefinitelyTyped to work in dom-less repos.
This failed in IE11, as remove is not available there. You have to do element.parentNode.removeChild(element). But typescript didn't complain as remove() is just declared on Element
(Porting microsoft/TypeScript#20175)
A lot of teams have seen production bugs from trying to use code not available in older browsers.
performance.now
, for example, is listed inlib.d.ts
but shouldn't be used on sites that support IE10 and don't polyfillperformance
.Two other ways of getting around this class of problem without dropping browsers or adding polyfills (are there other good ones?) are:
"dom"
from"lib"
intsconfig.json
Removing
"dom"
typings works great to enforce using only non-browser APIs (or dependency-injecting them where used), which is much better than calling APIs likeperformance.now
andsetTimeout
willy nilly. However, you now need to manually re-write typings for all these native things as you go.The solution would be much simpler if TypeScript exposed a form of
lib.d.ts
that included only the interfaces and types for all these constructs without thedeclare var
/declare function
.Using setTimeout as an example, it's declared without a separate ISetTimeout interface in TypeScript's lib.d.ts:
If it were changed to provide an
ISetTimeout
it would be much easier to usesetTimeout
without a reliance on the browser+Node API.Errata: I personally remove
"dom"
whenever possible.. but a lot of@types
definitions assume it exists and it's a bit like whack-a-mole trying to fix typings on DefinitelyTyped to work in dom-less repos.Edit: some DefinitelyTyped issues this causes:
The text was updated successfully, but these errors were encountered: