lib.d.ts types are too loose: provide a version without vars declared?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- typescript
- Domain
- tooling
Research direction
No files, tests, or entry points are named. Start by tracing how this generator produces lib.d.ts declarations, then determine how a types-only variant could be generated while preserving the existing global declarations. Done means the generator exposes the requested separation and its generated output validates the browser-versus-types-only use case described here.
Written by the indexing model from the issue text.
Description
(Porting https://github.com/Microsoft/TypeScript/issues/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 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"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 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:
declare function setTimeout(handler: (...args: any[]) => void, timeout: number): number;
declare function setTimeout(handler: any, timeout?: any, ...args: any[]): number;
interface WindowTimers extends Object, WindowTimersExtension {
// ...
setTimeout(handler: (...args: any[]) => void, timeout: number): number;
setTimeout(handler: any, timeout?: any, ...args: any[]): number;
}
If it were changed to provide an ISetTimeout it would be much easier to use setTimeout without a reliance on the browser+Node API.
interface SetTimeout {
(handler: (...args: any[]) => void, timeout: number): number;
(handler: any, timeout?: any, ...args: any[]): number;
}
interface WindowTimers extends Object, WindowTimersExtension {
// ...
setTimeout: SetTimeout;
}
// Only in the standard ("loose") version:
declare var setTimeout: 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.
Edit: some DefinitelyTyped issues this causes:
- Dominant language
- TypeScript
- Stars
- 740
- Forks
- 474
- Avg merge
- 2d 8h
- Merged PRs (30d)
- 15
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/TypeScript-DOM-lib-generator
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
microsoft/TypeScript-DOM-lib-generator#2554 · 6 comments ·
-
lib.d.ts bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
lib.d.ts bug
Difficulty 1/5 Under an hour Newbie friendliness 68/100
microsoft/TypeScript-DOM-lib-generator#2154 · 1 reaction ·
-
lib.d.ts bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
microsoft/TypeScript-DOM-lib-generator#2107 · 5 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
microsoft/TypeScript-DOM-lib-generator#1675 · 2 comments · 1 reaction ·
All issues in microsoft/TypeScript-DOM-lib-generator
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
copse-dev/agent-pane#2953 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Eynzof/Hermes-CN-Desktop#610 ·
-
bug clawsweeper:linked-pr-open clawsweeper:needs-live-repro clawsweeper:no-new-fix-pr impact:message-loss issue-rating: 🐚 platinum hermit P2 regression
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
calcite-components needs triage refactor
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Esri/calcite-design-system#15203 ·