Skip to content

Commit d0b7ef3

Browse files
authored
feat(types/jsx): support jsxImportSource, avoid global JSX conflict (#7958)
- No longer implicitly register global JSX types by default - This avoid conflict when using Vue in the same project with React - Global registration must now be done by explicitly importing / referencing `vue/jsx`, or listing it in `compilerOptions.types`. - Add `vue/jsx-runtime` to support `jsxImportSource` usage - Can enable globally by setting `compilerOptions.jsxImportSource` to `'vue'` - Can also opt-in per-file with `/** @jsxImportSource vue */`
1 parent 9dd98f0 commit d0b7ef3

File tree

9 files changed

+83
-43
lines changed

9 files changed

+83
-43
lines changed

packages/dts-test/utils.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// This directory contains a number of d.ts assertions
22
// use \@ts-expect-error where errors are expected.
33

4+
// register global JSX
5+
import 'vue/jsx'
6+
47
export function describe(_name: string, _fn: () => void): void
58
export function test(_name: string, _fn: () => any): void
69

packages/runtime-dom/types/jsx.d.ts renamed to packages/vue/jsx-runtime/dom.d.ts

+1-42
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
// Kanitkorn Sujautra <https://github.com/lukyth>
2727
// Sebastian Silbermann <https://github.com/eps1lon>
2828

29-
import { VNode } from '@vue/runtime-core'
3029
import * as CSS from 'csstype'
3130

3231
export interface CSSProperties
@@ -1021,7 +1020,7 @@ export interface SVGAttributes extends AriaAttributes, EventHandlers<Events> {
10211020
zoomAndPan?: string
10221021
}
10231022

1024-
interface IntrinsicElementAttributes {
1023+
export interface IntrinsicElementAttributes {
10251024
a: AnchorHTMLAttributes
10261025
abbr: HTMLAttributes
10271026
address: HTMLAttributes
@@ -1320,43 +1319,3 @@ type EventHandlers<E> = {
13201319
? E[K]
13211320
: (payload: E[K]) => void
13221321
}
1323-
1324-
// use namespace import to avoid collision with generated types which use
1325-
// named imports.
1326-
import * as RuntimeCore from '@vue/runtime-core'
1327-
1328-
type ReservedProps = {
1329-
key?: string | number | symbol
1330-
ref?: RuntimeCore.VNodeRef
1331-
ref_for?: boolean
1332-
ref_key?: string
1333-
}
1334-
1335-
type ElementAttrs<T> = T & ReservedProps
1336-
1337-
type NativeElements = {
1338-
[K in keyof IntrinsicElementAttributes]: ElementAttrs<
1339-
IntrinsicElementAttributes[K]
1340-
>
1341-
}
1342-
1343-
declare global {
1344-
namespace JSX {
1345-
interface Element extends VNode {}
1346-
interface ElementClass {
1347-
$props: {}
1348-
}
1349-
interface ElementAttributesProperty {
1350-
$props: {}
1351-
}
1352-
interface IntrinsicElements extends NativeElements {
1353-
// allow arbitrary elements
1354-
// @ts-ignore suppress ts:2374 = Duplicate string index signature.
1355-
[name: string]: any
1356-
}
1357-
interface IntrinsicAttributes extends ReservedProps {}
1358-
}
1359-
}
1360-
1361-
// suppress ts:2669
1362-
export {}

packages/vue/jsx-runtime/index.d.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { VNode, VNodeRef } from '@vue/runtime-dom'
2+
import { IntrinsicElementAttributes } from './dom'
3+
4+
export type ReservedProps = {
5+
key?: string | number | symbol
6+
ref?: VNodeRef
7+
ref_for?: boolean
8+
ref_key?: string
9+
}
10+
11+
export type NativeElements = {
12+
[K in keyof IntrinsicElementAttributes]: IntrinsicElementAttributes[K] &
13+
ReservedProps
14+
}
15+
16+
/**
17+
* JSX namespace for usage with @jsxImportsSource directive
18+
* when ts compilerOptions.jsx is 'react-jsx' or 'react-jsxdev'
19+
* https://www.typescriptlang.org/tsconfig#jsxImportSource
20+
*/
21+
export { h as jsx, h as jsxDEV, Fragment } from '@vue/runtime-dom'
22+
23+
export namespace JSX {
24+
export interface Element extends VNode {}
25+
export interface ElementClass {
26+
$props: {}
27+
}
28+
export interface ElementAttributesProperty {
29+
$props: {}
30+
}
31+
export interface IntrinsicElements extends NativeElements {
32+
// allow arbitrary elements
33+
// @ts-ignore suppress ts:2374 = Duplicate string index signature.
34+
[name: string]: any
35+
}
36+
export interface IntrinsicAttributes extends ReservedProps {}
37+
export interface ElementChildrenAttribute {
38+
$slots: {}
39+
}
40+
}

packages/vue/jsx-runtime/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const Vue = require('vue')
2+
exports.jsx = Vue.h
3+
exports.jsxDEV = Vue.h
4+
exports.Fragment = Vue.Fragment

packages/vue/jsx-runtime/index.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { h as jsx, h as jsxDEV, Fragment } from 'vue'

packages/vue/jsx-runtime/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"main": "index.js",
3+
"module": "index.mjs",
4+
"types": "index.d.ts"
5+
}

packages/vue/jsx.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// global JSX namespace registration
2+
import { JSX as JSXInternal } from './jsx-runtime'
3+
4+
declare global {
5+
namespace JSX {
6+
interface Element extends JSXInternal.Element {}
7+
interface ElementClass extends JSXInternal.ElementClass {}
8+
interface ElementAttributesProperty
9+
extends JSXInternal.ElementAttributesProperty {}
10+
interface IntrinsicElements extends JSXInternal.IntrinsicElements {}
11+
interface IntrinsicAttributes extends JSXInternal.IntrinsicAttributes {}
12+
interface ElementChildrenAttribute
13+
extends JSXInternal.ElementChildrenAttribute {}
14+
}
15+
}
16+
17+
export {}

packages/vue/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"dist",
1414
"compiler-sfc",
1515
"server-renderer",
16+
"jsx-runtime",
17+
"jsx.d.ts",
1618
"macros.d.ts",
1719
"macros-global.d.ts",
1820
"ref-macros.d.ts"
@@ -36,6 +38,14 @@
3638
"import": "./compiler-sfc/index.mjs",
3739
"require": "./compiler-sfc/index.js"
3840
},
41+
"./jsx-runtime": {
42+
"types": "./jsx-runtime/index.d.ts",
43+
"import": "./jsx-runtime/index.mjs",
44+
"require": "./jsx-runtime/index.js"
45+
},
46+
"./jsx": {
47+
"types": "./jsx.d.ts"
48+
},
3949
"./dist/*": "./dist/*",
4050
"./package.json": "./package.json",
4151
"./macros": "./macros.d.ts",

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"packages/*/src",
3333
"packages/runtime-dom/types/jsx.d.ts",
3434
"packages/*/__tests__",
35-
"packages/dts-test"
35+
"packages/dts-test",
36+
"packages/vue/jsx-runtime"
3637
]
3738
}

0 commit comments

Comments
 (0)