Skip to content

Commit

Permalink
feat(jsx): improve a-tag types with well known values (#3287)
Browse files Browse the repository at this point in the history
* refactor: LiteralUnion type moves to utils

* feat(jsx): improve a-tag types with well known values
  • Loading branch information
ssssota authored Aug 19, 2024
1 parent 1854e24 commit 17c3b9e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/jsx/intrinsic-elements.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import type { BaseMime } from '../utils/mime'
import type { StringLiteralUnion } from '../utils/types'

/**
* This code is based on React.
* https://github.com/facebook/react
Expand Down Expand Up @@ -199,7 +202,7 @@ export namespace JSX {
| 'strict-origin-when-cross-origin'
| 'unsafe-url'

type HTMLAttributeAnchorTarget = '_self' | '_blank' | '_parent' | '_top' | string
type HTMLAttributeAnchorTarget = StringLiteralUnion<'_self' | '_blank' | '_parent' | '_top'>

interface AnchorHTMLAttributes extends HTMLAttributes {
download?: string | boolean | undefined
Expand All @@ -208,7 +211,7 @@ export namespace JSX {
media?: string | undefined
ping?: string | undefined
target?: HTMLAttributeAnchorTarget | undefined
type?: string | undefined
type?: StringLiteralUnion<BaseMime> | undefined
referrerpolicy?: HTMLAttributeReferrerPolicy | undefined
}

Expand Down Expand Up @@ -466,11 +469,6 @@ export namespace JSX {
src?: string | undefined
}

/**
* String literal types with auto-completion
* @see https://github.com/Microsoft/TypeScript/issues/29729
*/
type LiteralUnion<T> = T | (string & Record<never, never>)
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#http-equiv
*/
Expand Down Expand Up @@ -519,15 +517,15 @@ export namespace JSX {
| 'og:image:height'
| 'og:image:alt'
interface MetaHTMLAttributes extends HTMLAttributes {
charset?: LiteralUnion<'utf-8'> | undefined
'http-equiv'?: LiteralUnion<MetaHttpEquiv> | undefined
name?: LiteralUnion<MetaName> | undefined
charset?: StringLiteralUnion<'utf-8'> | undefined
'http-equiv'?: StringLiteralUnion<MetaHttpEquiv> | undefined
name?: StringLiteralUnion<MetaName> | undefined
media?: string | undefined
content?: string | undefined
property?: LiteralUnion<MetaProperty> | undefined
property?: StringLiteralUnion<MetaProperty> | undefined

// React 19 compatibility
httpEquiv?: LiteralUnion<MetaHttpEquiv> | undefined
httpEquiv?: StringLiteralUnion<MetaHttpEquiv> | undefined
}

interface MeterHTMLAttributes extends HTMLAttributes {
Expand Down
6 changes: 6 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ export type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType>
: true

export type IsAny<T> = boolean extends (T extends never ? true : false) ? true : false

/**
* String literal types with auto-completion
* @see https://github.com/Microsoft/TypeScript/issues/29729
*/
export type StringLiteralUnion<T> = T | (string & Record<never, never>)

0 comments on commit 17c3b9e

Please sign in to comment.