Closed as not planned
Description
π Search Terms
WebIDL, Nominal, Structural, Interface, platform object
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
TypeScript should provide better support for "platform objects". Right now, it only checks them structurally, which:
- Falsely marks incorrect code as type-correct.
- Provides misleading error messages and Quick Fix suggestions
- Does more computational work than necessary
Instead, it should:
- check that platform object parameters are nominally the correct type (as it does for
unique symbol
types) - provide appropriate suggestions to fixing such mismatches
π Motivating Example
Take the following code:
/// <reference no-default-lib="true"/>
/// <reference lib="dom" />
const aSignal: AbortSignal = {}
fetch("http://example.com", {signal: aSignal})
This emits the error:
Type '{}' is missing the following properties from type 'AbortSignal': aborted, onabort, reason, throwIfAborted, and 3 more.(2740)
and provides a quick fix action to "add missing properties".
- Both are misleading. The problem is that
aSignal
is NOT a platform object and that its "inherited interfaces" (in the WebIDL sense) does not includeAbortSignal
. - Adding the "missing members" shows no errors, even though the code is still incorrect.
- Any work in checking if an argument implements
AbortSignal
need not structurally examine the value.
π» Use Cases
- What do you want to use this for?
- writing correct code
- What shortcomings exist with current approaches?
- See above
- What workarounds are you using in the meantime?
- See above