Skip to content

Commit b40fcbc

Browse files
committed
fix(types): augment ref unwrap bail types in appropriate packages
Packages can now augment the ref unwrap bail types in their own `d.ts`. Also updated the build script to auto concat any files in a package's `types` directory to the final generated `d.ts`. - `@vue/reactivity` should no longer require `libs: ["DOM"]` in tsconfig - Properly bail on `VNode` and `ComponentPublicInstance` in runtime-core
1 parent 7f23555 commit b40fcbc

File tree

10 files changed

+86
-12
lines changed

10 files changed

+86
-12
lines changed

packages/reactivity/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export {
99
triggerRef,
1010
Ref,
1111
UnwrapRef,
12-
ToRefs
12+
ToRefs,
13+
RefUnwrapBailTypes
1314
} from './ref'
1415
export {
1516
reactive,

packages/reactivity/src/ref.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,37 @@ export function toRef<T extends object, K extends keyof T>(
139139
// corner case when use narrows type
140140
// Ex. type RelativePath = string & { __brand: unknown }
141141
// RelativePath extends object -> true
142-
type BaseTypes = string | number | boolean | Node | Window
142+
type BaseTypes = string | number | boolean
143+
144+
/**
145+
* This is a special exported interface for other packages to declare
146+
* additional types that should bail out for ref unwrapping. For example
147+
* \@vue/runtime-dom can declare it like so in its d.ts:
148+
*
149+
* ``` ts
150+
* declare module '@vue/reactivity' {
151+
* export interface RefUnwrapBailTypes {
152+
* runtimeDOMBailTypes: Node | Window
153+
* }
154+
* }
155+
* ```
156+
*
157+
* Note that api-extractor somehow refuses to include `decalre module`
158+
* augmentations in its generated d.ts, so we have to manually append them
159+
* to the final generated d.ts in our build process.
160+
*/
161+
export interface RefUnwrapBailTypes {}
143162

144163
export type UnwrapRef<T> = T extends ComputedRef<infer V>
145164
? UnwrapRefSimple<V>
146165
: T extends Ref<infer V> ? UnwrapRefSimple<V> : UnwrapRefSimple<T>
147166

148-
type UnwrapRefSimple<T> = T extends Function | CollectionTypes | BaseTypes | Ref
167+
type UnwrapRefSimple<T> = T extends
168+
| Function
169+
| CollectionTypes
170+
| BaseTypes
171+
| Ref
172+
| RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
149173
? T
150174
: T extends Array<any> ? T : T extends object ? UnwrappedObject<T> : T
151175

packages/runtime-core/src/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ export {
8787

8888
// Types -----------------------------------------------------------------------
8989

90+
import { VNode } from './vnode'
91+
import { ComponentInternalInstance } from './component'
92+
93+
// Augment Ref unwrap bail types.
94+
// Note: if updating this, also update `types/refBail.d.ts`.
95+
declare module '@vue/reactivity' {
96+
export interface RefUnwrapBailTypes {
97+
runtimeCoreBailTypes:
98+
| VNode
99+
| {
100+
// directly bailing on ComponentPublicInstance results in recursion
101+
// so we use this as a bail hint
102+
$: ComponentInternalInstance
103+
}
104+
}
105+
}
106+
90107
export {
91108
ReactiveEffect,
92109
ReactiveEffectOptions,
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Note: this file is auto concatenated to the end of the bundled d.ts during
2+
// build.
3+
4+
declare module '@vue/reactivity' {
5+
export interface RefUnwrapBailTypes {
6+
runtimeCoreBailTypes:
7+
| VNode
8+
| {
9+
// directly bailing on ComponentPublicInstance results in recursion
10+
// so we use this as a bail hint
11+
$: ComponentInternalInstance
12+
}
13+
}
14+
}

packages/runtime-dom/package.json

-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
"esm-browser",
1919
"cjs",
2020
"global"
21-
],
22-
"dts": [
23-
"jsx.d.ts"
2421
]
2522
},
2623
"repository": {

packages/runtime-dom/src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import { patchProp } from './patchProp'
1414
// Importing from the compiler, will be tree-shaken in prod
1515
import { isFunction, isString, isHTMLTag, isSVGTag } from '@vue/shared'
1616

17+
declare module '@vue/reactivity' {
18+
export interface RefUnwrapBailTypes {
19+
// Note: if updating this, also update `types/refBail.d.ts`.
20+
runtimeDOMBailTypes: Node | Window
21+
}
22+
}
23+
1724
const rendererOptions = {
1825
patchProp,
1926
...nodeOps

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Note: this file is auto concatenated to the end of the bundled d.ts during
2+
// build.
3+
14
import { Ref, ComponentPublicInstance } from '@vue/runtime-core'
25

36
// This code is based on react definition in DefinitelyTyped published under the MIT license.
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Note: this file is auto concatenated to the end of the bundled d.ts during
2+
// build.
3+
4+
declare module '@vue/reactivity' {
5+
export interface RefUnwrapBailTypes {
6+
runtimeDOMBailTypes: Node | Window
7+
}
8+
}

scripts/build.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,15 @@ async function build(target) {
107107
})
108108

109109
if (extractorResult.succeeded) {
110-
// concat additional d.ts to rolled-up dts (mostly for JSX)
111-
if (pkg.buildOptions && pkg.buildOptions.dts) {
110+
// concat additional d.ts to rolled-up dts
111+
const typesDir = path.resolve(pkgDir, 'types')
112+
if (await fs.exists(typesDir)) {
112113
const dtsPath = path.resolve(pkgDir, pkg.types)
113114
const existing = await fs.readFile(dtsPath, 'utf-8')
115+
const typeFiles = await fs.readdir(typesDir)
114116
const toAdd = await Promise.all(
115-
pkg.buildOptions.dts.map(file => {
116-
return fs.readFile(path.resolve(pkgDir, file), 'utf-8')
117+
typeFiles.map(file => {
118+
return fs.readFile(path.resolve(typesDir, file), 'utf-8')
117119
})
118120
)
119121
await fs.writeFile(dtsPath, existing + '\n' + toAdd.join('\n'))

tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
"rootDir": ".",
2020
"paths": {
2121
"@vue/*": ["packages/*/src"],
22-
"vue": ["packages/vue/src"]
22+
"vue": ["packages/vue/src"],
23+
"@vue/reavitity": ["packages/reactivity/src/index.ts"]
2324
}
2425
},
2526
"include": [
2627
"packages/global.d.ts",
27-
"packages/runtime-dom/jsx.d.ts",
2828
"packages/*/src",
29+
"packages/runtime-dom/types/jsx.d.ts",
2930
"packages/*/__tests__",
3031
"test-dts"
3132
]

0 commit comments

Comments
 (0)