Typescript: Separate read and write types for properties

Created on 12 Aug 2019  ·  5Comments  ·  Source: microsoft/TypeScript

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

https://github.com/microsoft/TypeScript/issues/590
https://github.com/microsoft/TypeScript/issues/814

Use Cases

Use DOM APIs :)

Also see: https://github.com/runem/lit-analyzer/issues/43

Examples

Shown above

Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
Needs Proposal Suggestion

Most helpful comment

We're halfway there as of last Friday and we're looking into expanding it in 3.7. In 3.6 we should parse getters and setters on ambient class declarations, in 3.7 we intend to expand to object and interface types and include better checking rules for them.

All 5 comments

@DanielRosenwasser had mentioned this idea as an option for solving https://github.com/runem/lit-analyzer/issues/43

We're halfway there as of last Friday and we're looking into expanding it in 3.7. In 3.6 we should parse getters and setters on ambient class declarations, in 3.7 we intend to expand to object and interface types and include better checking rules for them.

(Although I don't think we have any backlog item for specifically differing getter/setter types yet)

I don't think it should be limited to properties.

We should consider if there's a way to work with divergent bounds on types. We usually think of a type being lower-bounded and upper-bounded by two types (LowerTUpper), but I don't know if we've thought much about TLesserUpper _and_ GreaterLowerT.

Maybe that's just intersections of ranged types?

(NeverTLesserUpper) & (GreaterLowerTUnknown).

But that doesn't have any overlap, which is strange. Just spitballing here though.

Could we extend the duality to index access types?

Was this page helpful?
0 / 5 - 0 ratings