Skip to content

Commit 17c3b9e

Browse files
authored
feat(jsx): improve a-tag types with well known values (#3287)
* refactor: LiteralUnion type moves to utils * feat(jsx): improve a-tag types with well known values
1 parent 1854e24 commit 17c3b9e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/jsx/intrinsic-elements.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

3+
import type { BaseMime } from '../utils/mime'
4+
import type { StringLiteralUnion } from '../utils/types'
5+
36
/**
47
* This code is based on React.
58
* https://github.com/facebook/react
@@ -199,7 +202,7 @@ export namespace JSX {
199202
| 'strict-origin-when-cross-origin'
200203
| 'unsafe-url'
201204

202-
type HTMLAttributeAnchorTarget = '_self' | '_blank' | '_parent' | '_top' | string
205+
type HTMLAttributeAnchorTarget = StringLiteralUnion<'_self' | '_blank' | '_parent' | '_top'>
203206

204207
interface AnchorHTMLAttributes extends HTMLAttributes {
205208
download?: string | boolean | undefined
@@ -208,7 +211,7 @@ export namespace JSX {
208211
media?: string | undefined
209212
ping?: string | undefined
210213
target?: HTMLAttributeAnchorTarget | undefined
211-
type?: string | undefined
214+
type?: StringLiteralUnion<BaseMime> | undefined
212215
referrerpolicy?: HTMLAttributeReferrerPolicy | undefined
213216
}
214217

@@ -466,11 +469,6 @@ export namespace JSX {
466469
src?: string | undefined
467470
}
468471

469-
/**
470-
* String literal types with auto-completion
471-
* @see https://github.com/Microsoft/TypeScript/issues/29729
472-
*/
473-
type LiteralUnion<T> = T | (string & Record<never, never>)
474472
/**
475473
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#http-equiv
476474
*/
@@ -519,15 +517,15 @@ export namespace JSX {
519517
| 'og:image:height'
520518
| 'og:image:alt'
521519
interface MetaHTMLAttributes extends HTMLAttributes {
522-
charset?: LiteralUnion<'utf-8'> | undefined
523-
'http-equiv'?: LiteralUnion<MetaHttpEquiv> | undefined
524-
name?: LiteralUnion<MetaName> | undefined
520+
charset?: StringLiteralUnion<'utf-8'> | undefined
521+
'http-equiv'?: StringLiteralUnion<MetaHttpEquiv> | undefined
522+
name?: StringLiteralUnion<MetaName> | undefined
525523
media?: string | undefined
526524
content?: string | undefined
527-
property?: LiteralUnion<MetaProperty> | undefined
525+
property?: StringLiteralUnion<MetaProperty> | undefined
528526

529527
// React 19 compatibility
530-
httpEquiv?: LiteralUnion<MetaHttpEquiv> | undefined
528+
httpEquiv?: StringLiteralUnion<MetaHttpEquiv> | undefined
531529
}
532530

533531
interface MeterHTMLAttributes extends HTMLAttributes {

src/utils/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,9 @@ export type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType>
9797
: true
9898

9999
export type IsAny<T> = boolean extends (T extends never ? true : false) ? true : false
100+
101+
/**
102+
* String literal types with auto-completion
103+
* @see https://github.com/Microsoft/TypeScript/issues/29729
104+
*/
105+
export type StringLiteralUnion<T> = T | (string & Record<never, never>)

0 commit comments

Comments
 (0)