Skip to content

Add support for extracting the tag name in types #5

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

Closed
4 tasks done
wooorm opened this issue May 8, 2021 · 2 comments · Fixed by #6
Closed
4 tasks done

Add support for extracting the tag name in types #5

wooorm opened this issue May 8, 2021 · 2 comments · Fixed by #6
Labels
☂️ area/types This affects typings 💪 phase/solved Post is done 🦋 type/enhancement This is great to have 🙆 yes/confirmed This is confirmed and ready to be worked on

Comments

@wooorm
Copy link
Member

wooorm commented May 8, 2021

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and couldn’t find anything (or linked relevant results below)

Subject

Add support for extracting the tag name in types

Problem

h('a') // Element

Solution

h('a') // Element & {tagName: 'a'}

Something along these lines:

type DefaultTagName = 'div'
type ExtractTagName<T extends string> =
  T extends `${infer TagName}#${infer _}`
  ? (TagName extends '' ? DefaultTagName : ExtractTagName<TagName>)
  : T extends `${infer TagName}.${infer _}`
  ? (TagName extends '' ? DefaultTagName : TagName)
  : (T extends '' ? DefaultTagName : T); // No match

type A = ExtractTagName<'a'>;
type B = ExtractTagName<'#id'>;
type C = ExtractTagName<'.class'>;
type D = ExtractTagName<'a.red.blue'>;
type E = ExtractTagName<'a.class#id'>;
type F = ExtractTagName<'a#id.class'>;
type G = ExtractTagName<'a#'>;
type H = ExtractTagName<'.'>;

https://www.typescriptlang.org/play?ts=4.1.0-pr-40336-88#code/C4TwDgpgBAogHsATgQwMbACrIOYDlkC2EAPBlBAhAHYAmAzlHUgJZXYB8UAvAFBRRkKwavSgADACQBvVgDMIiATnxEAvgGJpchVAD6qsXygB+KAAoseQtCEiGAcnsmo9mswBuTgFywEKdJYqJIHW7ACURj6ClLQMkjJU8oohagB0Wok6+ob8phbK1uQxoo7Orh7eSlZEEfw+FkXCsS5OpuWeUFFhANxQAPR9ULgA9lAEyMCoABY8PKCQUACC3L5IaJgFRMT2dGDIVPbs3XPg0ABCK-BrAZsk9urMNIfH89AACpd+6yl3qagANsg6HRnicFgBhT7XDbVO4AI1SiAgNFScP+AFcIKDXlAAGJQ-wwoLbIEogFAugPJ5HWazIA

Alternatives

n/a

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels May 8, 2021
@wooorm wooorm added ☂️ area/types This affects typings 👍 phase/yes Post is accepted and can be worked on 🙆 yes/confirmed This is confirmed and ready to be worked on 🦋 type/enhancement This is great to have and removed 🤞 phase/open Post is being triaged manually labels May 8, 2021
@github-actions

This comment has been minimized.

wooorm added a commit that referenced this issue May 8, 2021
@wooorm wooorm closed this as completed in #6 May 9, 2021
wooorm added a commit that referenced this issue May 9, 2021
Closes GH-5.
Closes GH-6.

Reviewed-by: Christian Murphy <[email protected]>
@github-actions

This comment has been minimized.

@wooorm wooorm added the 💪 phase/solved Post is done label May 9, 2021
@github-actions github-actions bot removed the 👍 phase/yes Post is accepted and can be worked on label May 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☂️ area/types This affects typings 💪 phase/solved Post is done 🦋 type/enhancement This is great to have 🙆 yes/confirmed This is confirmed and ready to be worked on
Development

Successfully merging a pull request may close this issue.

1 participant