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

Alias $type inheritance? #236

Open
drwpow opened this issue Apr 9, 2024 · 2 comments
Open

Alias $type inheritance? #236

drwpow opened this issue Apr 9, 2024 · 2 comments

Comments

@drwpow
Copy link

drwpow commented Apr 9, 2024

Was digging into alias behavior a bit, and I had a few question about alias $types. What are peoples’ thoughts on whether any of the following are valid or invalid? Also I may have missed a previous conversation or part of the spec that applies to this.

Omitting $type

Is this valid?

{
  "color": {
    "a": { "$type": "color", "$value": "#336699" },
    "b": { "$value": "{color.a}" }
}

$type mismatch

Will color.b be a color, and be validated as such (sort of an extension of the previous one)?

{
  "color": {
    "a": { "$type": "color", "$value": "#336699" },
    "b": { "$type": "dimension", "$value": "{color.a}" }
}

And an even bigger question: is declaring ANY $type on an alias invalid?

$type inheritance

Will the inherited $type also apply to the alias? (and override any possible $type the alias may have)?

{
  "base": {
    "$type": "color",
    "a": { "$value": "#336699" }
  },
  "semantic": { "$value": "{base.a}" }
}

In all of these, I’d like to reduce error as much as possible, and I can’t think of a good reason for even declaring $type on aliases because there’s at best there’s duplication; at worst there’s the possibility of a conflict. And wondering when it’s helpful to err, when it’s helpful to warn, and when it’s helpful to do nothing (silently work)

@whoisryosuke
Copy link

I think the $type alias is to allow for parsers just to know to check an object and its properties. Otherwise how would it know to check for colors? It’d have to assume a token structure (like all colors are under top level color property).

It also helps explicitly define the type of the token, since you might want to mix tokens to achieve another (like combining opacity number and RGB color to make a RGBA new color). Or for example, if you multiply fontSize and a spacing token, what is the type of the result? Might get weird for parsers.

I do think there you have a great idea of not including it every time though. It seems verbose and makes it difficult to manually parse JSON without a tool to strip the noise. Not sure how to practically achieve that without an explicit structure or some other system to accommodate for the inference of types.

Also this issue emphasizes the need for a section of the docs on parsing, and expectations the spec has - to create some consistency between tooling.

@drwpow
Copy link
Author

drwpow commented Apr 12, 2024

I think the $type alias is to allow for parsers just to know to check an object and its properties. Otherwise how would it know to check for colors? It’d have to assume a token structure (like all colors are under top level color property).

Well that’s just the thing though—parsers actually have to validate aliases currently. Aliases have to be non-circular. And non-aliases have to be valid tokens. By just accepting an invalid schema at face-value, the user will get errors with no clear action on how to fix it. So parsers actually do have to scan the alias and know it’s referring to a color anyway, and there’s no way for them to avoid having that work (or they’d be bad, hard-to-use-tools). But I was more interested in exploring whether that’s an error to show to the user “fix this!” or if it just silently works

Also this issue emphasizes the need for a section of the docs on parsing, and expectations the spec has - to create some consistency between tooling.

💯!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants