-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for tag name inferral in types
- Loading branch information
Showing
7 changed files
with
126 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
||
export type ExtractTagName<X, Y> = string | ||
|
||
/* eslint-enable @typescript-eslint/no-unused-vars */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
||
export type ExtractTagName< | ||
SimpleSelector extends string, | ||
DefaultTagName extends string | ||
> = SimpleSelector extends `#${infer Rest}` | ||
? DefaultTagName | ||
: SimpleSelector extends `.${infer Rest}` | ||
? DefaultTagName | ||
: SimpleSelector extends `${infer TagName}.${infer Rest}` | ||
? ExtractTagName<TagName, DefaultTagName> | ||
: SimpleSelector extends `${infer TagName}#${infer Rest}` | ||
? TagName | ||
: SimpleSelector extends '' | ||
? DefaultTagName | ||
: SimpleSelector extends string | ||
? SimpleSelector | ||
: DefaultTagName | ||
|
||
/* eslint-enable @typescript-eslint/no-unused-vars */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {expectType, expectAssignable} from 'tsd' | ||
import {Element} from 'hast' | ||
import {parseSelector} from './index.js' | ||
|
||
type A = Element & {tagName: 'a'} | ||
type Div = Element & {tagName: 'div'} | ||
type G = Element & {tagName: 'g'} | ||
|
||
// No tag name. | ||
expectType<Div>(parseSelector('')) | ||
expectType<Div>(parseSelector('#id')) | ||
expectType<Div>(parseSelector('.class')) | ||
expectType<Div>(parseSelector('#id.class')) | ||
expectType<Div>(parseSelector('.class#id')) | ||
|
||
// A tag name. | ||
expectType<A>(parseSelector('a')) | ||
expectType<A>(parseSelector('a#id')) | ||
expectType<A>(parseSelector('a.class')) | ||
expectType<A>(parseSelector('a#id.class')) | ||
expectType<A>(parseSelector('a.class#id')) | ||
|
||
// A default tag name | ||
expectType<G>(parseSelector('', 'g')) | ||
expectType<G>(parseSelector('#id', 'g')) | ||
expectType<G>(parseSelector('.class', 'g')) | ||
expectType<G>(parseSelector('#id.class', 'g')) | ||
expectType<G>(parseSelector('.class#id', 'g')) | ||
|
||
// They’re still elements. | ||
expectAssignable<Element>(parseSelector('')) | ||
expectAssignable<Element>(parseSelector('', 'g')) | ||
expectAssignable<Element>(parseSelector('a')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters