Skip to content

Support WebIDL interfaces nominally (not just structurally)Β #59971

Closed as not planned
@rotu

Description

@rotu

πŸ” Search Terms

WebIDL, Nominal, Structural, Interface, platform object

βœ… Viability Checklist

⭐ Suggestion

TypeScript should provide better support for "platform objects". Right now, it only checks them structurally, which:

  1. Falsely marks incorrect code as type-correct.
  2. Provides misleading error messages and Quick Fix suggestions
  3. Does more computational work than necessary

Instead, it should:

  1. check that platform object parameters are nominally the correct type (as it does for unique symbol types)
  2. 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".

  1. Both are misleading. The problem is that aSignal is NOT a platform object and that its "inherited interfaces" (in the WebIDL sense) does not include AbortSignal.
  2. Adding the "missing members" shows no errors, even though the code is still incorrect.
  3. Any work in checking if an argument implements AbortSignal need not structurally examine the value.

πŸ’» Use Cases

  1. What do you want to use this for?
    • writing correct code
  2. What shortcomings exist with current approaches?
    • See above
  3. What workarounds are you using in the meantime?
    • See above

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions