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

feat(jsx): improve a-tag types with well known values #3287

Merged
merged 2 commits into from
Aug 19, 2024
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
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'>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which do you like better this or the above:

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

I reffered to DefinitelyTyped/DefinitelyTyped@462531a .


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>)