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

add JSX IntrinsicElement types #214

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"import/no-unresolved": "off"
},
"overrides": [
{
"files": "src/*-define.ts",
"rules": {
"@typescript-eslint/no-namespace": "off"
}
},
{
"files": "test/**/*.js",
"rules": {
Expand Down
8 changes: 8 additions & 0 deletions src/local-time-element-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ try {
}
}

type JSXBaseElement = JSX.IntrinsicElements extends {span: unknown}
? JSX.IntrinsicElements['span']
: Record<string, unknown>
Comment on lines +16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having a really hard time reading what this does 😅

JSXBaseElement is a the JSX element span if JSX elements extend {span: unknwon}? Otherwise it's just a record of string, unknown?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a guard that essentially checks if JSX.InstrinsicElements.span exists. If it doesn't exist, as you say, then the type is Record<string, unknown>.

In other words, if a user installs this webcomponent and doesn't have @types/react then this will create:

JSX.InstrinsicElements['relative-time'] = Record<string, unknown> & Partial<Omit<RelativeTimeElement, keyof HTMLElement>>

If they do have @types/react then this will create:

JSX.InstrinsicElements['relative-time'] = JSX.InstrinsicElements.span & Partial<Omit<RelativeTimeElement, keyof HTMLElement>>

declare global {
interface Window {
LocalTimeElement: typeof LocalTimeElement
}
interface HTMLElementTagNameMap {
'local-time': LocalTimeElement
}
namespace JSX {
interface IntrinsicElements {
['local-time']: JSXBaseElement & Partial<Omit<LocalTimeElement, keyof HTMLElement>>
}
}
}

export default LocalTimeElement
Expand Down
8 changes: 8 additions & 0 deletions src/relative-time-element-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ try {
}
}

type JSXBaseElement = JSX.IntrinsicElements extends {span: unknown}
? JSX.IntrinsicElements['span']
: Record<string, unknown>
declare global {
interface Window {
RelativeTimeElement: typeof RelativeTimeElement
}
interface HTMLElementTagNameMap {
'relative-time': RelativeTimeElement
}
namespace JSX {
interface IntrinsicElements {
['relative-time']: JSXBaseElement & Partial<Omit<RelativeTimeElement, keyof HTMLElement>>
}
}
}

export default RelativeTimeElement
Expand Down
8 changes: 8 additions & 0 deletions src/time-ago-element-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ try {
}
}

type JSXBaseElement = JSX.IntrinsicElements extends {span: unknown}
? JSX.IntrinsicElements['span']
: Record<string, unknown>
declare global {
interface Window {
TimeAgoElement: typeof TimeAgoElement
}
interface HTMLElementTagNameMap {
'time-ago': TimeAgoElement
}
namespace JSX {
interface IntrinsicElements {
['time-ago']: JSXBaseElement & Partial<Omit<TimeAgoElement, keyof HTMLElement>>
}
}
}

export default TimeAgoElement
Expand Down
8 changes: 8 additions & 0 deletions src/time-until-element-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ try {
}
}

type JSXBaseElement = JSX.IntrinsicElements extends {span: unknown}
? JSX.IntrinsicElements['span']
: Record<string, unknown>
declare global {
interface Window {
TimeUntilElement: typeof TimeUntilElement
}
interface HTMLElementTagNameMap {
'time-until': TimeUntilElement
}
namespace JSX {
interface IntrinsicElements {
['time-until']: JSXBaseElement & Partial<Omit<TimeUntilElement, keyof HTMLElement>>
}
}
}

export default TimeUntilElement
Expand Down