Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate read and write types for properties #32821

Closed
5 tasks done
justinfagnani opened this issue Aug 12, 2019 · 7 comments
Closed
5 tasks done

Separate read and write types for properties #32821

justinfagnani opened this issue Aug 12, 2019 · 7 comments
Assignees
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@justinfagnani
Copy link

justinfagnani commented Aug 12, 2019

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.
@justinfagnani
Copy link
Author

@DanielRosenwasser had mentioned this idea as an option for solving runem/lit-analyzer#43

@weswigham
Copy link
Member

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.

@weswigham
Copy link
Member

weswigham commented Aug 12, 2019

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

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Aug 12, 2019

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.

@jack-williams
Copy link
Collaborator

Could we extend the duality to index access types?

@RyanCavanaugh RyanCavanaugh added Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript labels Aug 13, 2019
@ExE-Boss
Copy link
Contributor

ExE-Boss commented Feb 5, 2021

This is possibly a duplicate of #2521

@justinfagnani
Copy link
Author

@ExE-Boss indeed. Closing this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants