Need way to express hybrid types that are indexable for a subset of properties
**Edit by @DanielRosenwasser**: This might be thought of as a "rest index signature" or a catch-all index signature. __________ This is a feature request. **TypeScript Version:** 2.4 **Code** ```ts interface CSSProperties { marginLeft?: string | number [key: string]: CSSProperties } ``` Based on the [docs](https://www.typescriptlang.org/docs/handbook/interfaces.html), this is not allowed: > While string index signatures are a powerful way to describe the “dictionary” pattern, they also enforce that all properties match their return type. This is because a string index declares that obj.property is also available as obj[“property”]. In the following example, name’s type does not match the string index’s type, and the type-checker gives an error: Unfortunately, it seems to make this type (which is common amongst jss-in-css solutions) not expressible. Coming from flow, which handles index types by assuming they refer to the properties that are not explicitly typed, this is frustrating. As it stands, you can workaround it with: ```ts interface CSSProperties { marginLeft?: string | number [key: string]: CSSProperties | string | number } ``` But this is not sound. It allows this: ```ts const style: CSSProperties = { margarineLeft: 3 } ``` This could potentially be solved with subtraction types if they allowed subtracting from `string` (and you were allowed to specify key types in this way): ```ts interface CSSProperties { marginLeft?: string | number } i
Posted by aaronjensen on Aug 17, 2017, 6:46 PM
Tags
Related issues
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
Need way to express hybrid types that are indexable for a subset of properties
**Edit by @DanielRosenwasser**: This might be thought of as a "rest index signature" or a catch-all index signature. __________ This is a feature request. **TypeScript Version:** 2.4 **Code** ```ts interface CSSProperties { marginLeft?: string | number [key: string]: CSSProperties } ``` Based on the [docs](https://www.typescriptlang.org/docs/handbook/interfaces.html), this is not allowed: > While string index signatures are a powerful way to describe the “dictionary” pattern, they also enforce that all properties match their return type. This is because a string index declares that obj.property is also available as obj[“property”]. In the following example, name’s type does not match the string index’s type, and the type-checker gives an error: Unfortunately, it seems to make this type (which is common amongst jss-in-css solutions) not expressible. Coming from flow, which handles index types by assuming they refer to the properties that are not explicitly typed, this is frustrating. As it stands, you can workaround it with: ```ts interface CSSProperties { marginLeft?: string | number [key: string]: CSSProperties | string | number } ``` But this is not sound. It allows this: ```ts const style: CSSProperties = { margarineLeft: 3 } ``` This could potentially be solved with subtraction types if they allowed subtracting from `string` (and you were allowed to specify key types in this way): ```ts interface CSSProperties { marginLeft?: string | number } i
Comments (0)
No comments yet. Start the discussion.