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
triggerRef,
Ref,
UnwrapRef,
- ToRefs
+ ToRefs,
+ RefUnwrapBailTypes
} from './ref'
export {
reactive,
// corner case when use narrows type
// Ex. type RelativePath = string & { __brand: unknown }
// RelativePath extends object -> true
-type BaseTypes = string | number | boolean | Node | Window
+type BaseTypes = string | number | boolean
+
+/**
+ * This is a special exported interface for other packages to declare
+ * additional types that should bail out for ref unwrapping. For example
+ * \@vue/runtime-dom can declare it like so in its d.ts:
+ *
+ * ``` ts
+ * declare module '@vue/reactivity' {
+ * export interface RefUnwrapBailTypes {
+ * runtimeDOMBailTypes: Node | Window
+ * }
+ * }
+ * ```
+ *
+ * Note that api-extractor somehow refuses to include `decalre module`
+ * augmentations in its generated d.ts, so we have to manually append them
+ * to the final generated d.ts in our build process.
+ */
+export interface RefUnwrapBailTypes {}
export type UnwrapRef<T> = T extends ComputedRef<infer V>
? UnwrapRefSimple<V>
: T extends Ref<infer V> ? UnwrapRefSimple<V> : UnwrapRefSimple<T>
-type UnwrapRefSimple<T> = T extends Function | CollectionTypes | BaseTypes | Ref
+type UnwrapRefSimple<T> = T extends
+ | Function
+ | CollectionTypes
+ | BaseTypes
+ | Ref
+ | RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
? T
: T extends Array<any> ? T : T extends object ? UnwrappedObject<T> : T
// Types -----------------------------------------------------------------------
+import { VNode } from './vnode'
+import { ComponentInternalInstance } from './component'
+
+// Augment Ref unwrap bail types.
+// Note: if updating this, also update `types/refBail.d.ts`.
+declare module '@vue/reactivity' {
+ export interface RefUnwrapBailTypes {
+ runtimeCoreBailTypes:
+ | VNode
+ | {
+ // directly bailing on ComponentPublicInstance results in recursion
+ // so we use this as a bail hint
+ $: ComponentInternalInstance
+ }
+ }
+}
+
export {
ReactiveEffect,
ReactiveEffectOptions,
--- /dev/null
+// Note: this file is auto concatenated to the end of the bundled d.ts during
+// build.
+
+declare module '@vue/reactivity' {
+ export interface RefUnwrapBailTypes {
+ runtimeCoreBailTypes:
+ | VNode
+ | {
+ // directly bailing on ComponentPublicInstance results in recursion
+ // so we use this as a bail hint
+ $: ComponentInternalInstance
+ }
+ }
+}
"esm-browser",
"cjs",
"global"
- ],
- "dts": [
- "jsx.d.ts"
]
},
"repository": {
// Importing from the compiler, will be tree-shaken in prod
import { isFunction, isString, isHTMLTag, isSVGTag } from '@vue/shared'
+declare module '@vue/reactivity' {
+ export interface RefUnwrapBailTypes {
+ // Note: if updating this, also update `types/refBail.d.ts`.
+ runtimeDOMBailTypes: Node | Window
+ }
+}
+
const rendererOptions = {
patchProp,
...nodeOps
+// Note: this file is auto concatenated to the end of the bundled d.ts during
+// build.
+
import { Ref, ComponentPublicInstance } from '@vue/runtime-core'
// This code is based on react definition in DefinitelyTyped published under the MIT license.
--- /dev/null
+// Note: this file is auto concatenated to the end of the bundled d.ts during
+// build.
+
+declare module '@vue/reactivity' {
+ export interface RefUnwrapBailTypes {
+ runtimeDOMBailTypes: Node | Window
+ }
+}
})
if (extractorResult.succeeded) {
- // concat additional d.ts to rolled-up dts (mostly for JSX)
- if (pkg.buildOptions && pkg.buildOptions.dts) {
+ // concat additional d.ts to rolled-up dts
+ const typesDir = path.resolve(pkgDir, 'types')
+ if (await fs.exists(typesDir)) {
const dtsPath = path.resolve(pkgDir, pkg.types)
const existing = await fs.readFile(dtsPath, 'utf-8')
+ const typeFiles = await fs.readdir(typesDir)
const toAdd = await Promise.all(
- pkg.buildOptions.dts.map(file => {
- return fs.readFile(path.resolve(pkgDir, file), 'utf-8')
+ typeFiles.map(file => {
+ return fs.readFile(path.resolve(typesDir, file), 'utf-8')
})
)
await fs.writeFile(dtsPath, existing + '\n' + toAdd.join('\n'))
"rootDir": ".",
"paths": {
"@vue/*": ["packages/*/src"],
- "vue": ["packages/vue/src"]
+ "vue": ["packages/vue/src"],
+ "@vue/reavitity": ["packages/reactivity/src/index.ts"]
}
},
"include": [
"packages/global.d.ts",
- "packages/runtime-dom/jsx.d.ts",
"packages/*/src",
+ "packages/runtime-dom/types/jsx.d.ts",
"packages/*/__tests__",
"test-dts"
]