Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Separate read and write types for properties #32821

Closed
@justinfagnani

Description

@justinfagnani

Search Terms

[read types and write types], [read type]

Suggestion

Many DOM APIs have setters that coerce values to strings. Using these APIs in perfectly valid ways will result in type warnings:

const input = document.createElement('input');
input.type = 'range';
input.max = 42; // Type '42' is not assignable to type 'string'.

These APIs could be more accurately described if getters and setters could have different types, like:

interface HTMLInputElement extends HTMLElement {
    /**
     * Defines the maximum acceptable value for an input element with type="number".When used with the min and step attributes, lets you control the range and increment (such as only even numbers) that the user can enter into an input field.
     */
    get max(): string;
    set max(v: any);
}

Related Issues

#590
#814

Use Cases

Use DOM APIs :)

Also see: runem/lit-analyzer#43

Examples

Shown above

Checklist

My suggestion meets these guidelines:

  • 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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Labels

Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions