question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need way to express hybrid types that are indexable for a subset of properties

See original GitHub issue

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

interface CSSProperties {
  marginLeft?: string | number
  [key: string]: CSSProperties
}

Based on the docs, 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:

interface CSSProperties {
  marginLeft?: string | number
  [key: string]: CSSProperties | string | number
}

But this is not sound. It allows this:

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):

interface CSSProperties {
  marginLeft?: string | number
}

interface NestedCSSProperties extends CSSProperties {
  [key: string - keyof CSSProperties]: CSSProperties
}

I asked about this on stackoverflow to confirm that I wasn’t missing something. It seems I’m not, so I guess I’d consider this a suggestion/discussion starter, since it’s probably not a “bug”. Thanks!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:218
  • Comments:36 (8 by maintainers)

github_iconTop GitHub Comments

20reactions
masaeeducommented, Nov 13, 2017

Perhaps we could use subtraction types to express this. { [P in string - "marginLeft"]: CSSProperties }?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Interfaces - TypeScript
Indexable Types ​​ Above, we have a StringArray interface that has an index signature. This index signature states that when a StringArray is...
Read more >
Understanding and using interfaces in TypeScript
Indexable types are helpful when you have to define custom properties and functions that should operate on a range of values of the...
Read more >
Hybrid Attributes — SQLAlchemy 2.0 Documentation
“hybrid” means the attribute has distinct behaviors defined at the class level and at the instance level.
Read more >
Cloud hybrid search service (Cloud SSA) FAQ - Microsoft Learn
Two way is typically the desired state of hybrid federated search ... You no longer have to worry about the size of your...
Read more >
VLDB and Partitioning Guide - Oracle Help Center
Figure 2-1 offers a graphical view of how partitioned tables differ from ... There are certain situations when you would want to partition...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found