Separate read and write types for properties
See original GitHub issueSearch 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:
- 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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Playground Example - Separate Write vs Read on Properties
Separate Write vs Read on Properties ... With TS 4.3, you can now have different set types vs the get for a particular...
Read more >How to declare and use read write properties - C# ...
This sample shows a Person class that has two properties: Name (string) and Age (int). Both properties provide get and set accessors, so...
Read more >TypeScript 4.3 Adds Separate Property Write Types
TypeScript 4.3 now allows you to specify types for reading and writing to properties, including new syntax for interfaces/object types to ...
Read more >Best way to separate read and write concerns using interfaces?
A class that has a public or protected property that provides only a set accessor will raise Code Analysis warning CA1044: Properties should...
Read more >Read/write capacity mode - Amazon DynamoDB
Reserve provisioned throughput capacity for reads and writes when ... Read request units and write request units; Peak traffic and scaling properties ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Could we extend the duality to index access types?