From 5ee6449b0160df775de01309638a037a834d3de5 Mon Sep 17 00:00:00 2001 From: yuhengshen Date: Fri, 11 Jul 2025 13:47:18 +0800 Subject: [PATCH] fix(types): [other] cannot find declare module types from installed packages (#21266) * fix(types): cannot find declare module types from installed packages * fix(types): play global type error * fix(types): typecheck error --- internal/build/src/tasks/types-definitions.ts | 6 ++- packages/utils/vue/props/util.ts | 53 ------------------- packages/utils/vue3.3.polyfill.ts | 51 ++++++++++++++++++ tsconfig.play.json | 1 + tsconfig.vitest.json | 7 ++- tsconfig.web.json | 6 ++- 6 files changed, 68 insertions(+), 56 deletions(-) create mode 100644 packages/utils/vue3.3.polyfill.ts diff --git a/internal/build/src/tasks/types-definitions.ts b/internal/build/src/tasks/types-definitions.ts index d354bdb11a..381b898c90 100644 --- a/internal/build/src/tasks/types-definitions.ts +++ b/internal/build/src/tasks/types-definitions.ts @@ -67,7 +67,11 @@ export const generateTypesDefinitions = async () => { ) const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }) - const formattedText = printer.printFile(sourceFile) + let formattedText = printer.printFile(sourceFile) + + // delete when upgrade to vue 3.3 + // insert import statement at the beginning of the file + formattedText = `import "./utils/vue3.3.polyfill";\n${formattedText}` await writeFile(entryFilePath, formattedText, 'utf8') diff --git a/packages/utils/vue/props/util.ts b/packages/utils/vue/props/util.ts index db91882e45..01c41a105c 100644 --- a/packages/utils/vue/props/util.ts +++ b/packages/utils/vue/props/util.ts @@ -1,5 +1,3 @@ -import type { Prop } from 'vue' - export type Writable = { -readonly [P in keyof T]: T[P] } export type WritableArray = T extends readonly any[] ? Writable : T @@ -9,55 +7,4 @@ export type IfUnknown = [unknown] extends [T] ? Y : N export type UnknownToNever = IfUnknown -// delete when upgrade to vue 3.3 : start -// __ExtractPublicPropTypes copy from vue 3.3 -// If the type T accepts type "any", output type Y, otherwise output type N. -// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360 -export type IfAny = 0 extends 1 & T ? Y : N - -type InferPropType = [T] extends [null] - ? NullAsAny extends true - ? any - : null - : [T] extends [{ type: null | true }] - ? any // As TS issue https://github.com/Microsoft/TypeScript/issues/14829 // somehow `ObjectConstructor` when inferred from { (): T } becomes `any` // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean` - : [T] extends [ObjectConstructor | { type: ObjectConstructor }] - ? Record - : [T] extends [BooleanConstructor | { type: BooleanConstructor }] - ? boolean - : [T] extends [DateConstructor | { type: DateConstructor }] - ? Date - : [T] extends [(infer U)[] | { type: (infer U)[] }] - ? U extends DateConstructor - ? Date | InferPropType - : InferPropType - : [T] extends [Prop] - ? unknown extends V - ? keyof V extends never - ? IfAny - : V - : V - : T - -type PublicRequiredKeys = { - [K in keyof T]: T[K] extends { required: true } ? K : never -}[keyof T] - -type PublicOptionalKeys = Exclude> - -/** - * Extract prop types from a runtime props options object. - * The extracted types are **public** - i.e. the expected props that can be - * passed to component. - */ - -declare module 'vue' { - // compatible with higher versions of Vue - export type __ExtractPublicPropTypes = { - [K in keyof Pick>]: InferPropType - } & { - [K in keyof Pick>]?: InferPropType - } -} -// delete when upgrade to vue 3.3 : end export {} diff --git a/packages/utils/vue3.3.polyfill.ts b/packages/utils/vue3.3.polyfill.ts new file mode 100644 index 0000000000..54553d1531 --- /dev/null +++ b/packages/utils/vue3.3.polyfill.ts @@ -0,0 +1,51 @@ +import type { Prop } from 'vue' +// delete when upgrade to vue 3.3 : start +// __ExtractPublicPropTypes copy from vue 3.3 +// If the type T accepts type "any", output type Y, otherwise output type N. +// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360 +type IfAny = 0 extends 1 & T ? Y : N + +type InferPropType = [T] extends [null] + ? NullAsAny extends true + ? any + : null + : [T] extends [{ type: null | true }] + ? any // As TS issue https://github.com/Microsoft/TypeScript/issues/14829 // somehow `ObjectConstructor` when inferred from { (): T } becomes `any` // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean` + : [T] extends [ObjectConstructor | { type: ObjectConstructor }] + ? Record + : [T] extends [BooleanConstructor | { type: BooleanConstructor }] + ? boolean + : [T] extends [DateConstructor | { type: DateConstructor }] + ? Date + : [T] extends [(infer U)[] | { type: (infer U)[] }] + ? U extends DateConstructor + ? Date | InferPropType + : InferPropType + : [T] extends [Prop] + ? unknown extends V + ? keyof V extends never + ? IfAny + : V + : V + : T + +type PublicRequiredKeys = { + [K in keyof T]: T[K] extends { required: true } ? K : never +}[keyof T] + +type PublicOptionalKeys = Exclude> + +declare module 'vue' { + // compatible with higher versions of Vue + /** + * Extract prop types from a runtime props options object. + * The extracted types are **public** - i.e. the expected props that can be + * passed to component. + */ + export type __ExtractPublicPropTypes = { + [K in keyof Pick>]: InferPropType + } & { + [K in keyof Pick>]?: InferPropType + } +} +// delete when upgrade to vue 3.3 : end diff --git a/tsconfig.play.json b/tsconfig.play.json index e3ab3e26ad..fa9155fb67 100644 --- a/tsconfig.play.json +++ b/tsconfig.play.json @@ -5,6 +5,7 @@ "lib": ["ESNext", "DOM", "DOM.Iterable"] }, "include": [ + "packages/utils/vue3.3.polyfill.ts", "packages", "typings/global.d.ts", "typings/env.d.ts", diff --git a/tsconfig.vitest.json b/tsconfig.vitest.json index e9adc59ab1..d459854841 100644 --- a/tsconfig.vitest.json +++ b/tsconfig.vitest.json @@ -6,6 +6,11 @@ "types": ["node", "jsdom", "unplugin-vue-macros/macros-global"], "skipLibCheck": true }, - "include": ["packages", "vitest.setup.ts", "typings/env.d.ts"], + "include": [ + "packages/utils/vue3.3.polyfill.ts", + "packages", + "vitest.setup.ts", + "typings/env.d.ts" + ], "exclude": ["node_modules", "dist", "**/*.md"] } diff --git a/tsconfig.web.json b/tsconfig.web.json index 4146dcd5d9..5a5df27866 100644 --- a/tsconfig.web.json +++ b/tsconfig.web.json @@ -7,7 +7,11 @@ "types": ["unplugin-vue-macros/macros-global"], "skipLibCheck": true }, - "include": ["packages", "typings/env.d.ts"], + "include": [ + "packages/utils/vue3.3.polyfill.ts", + "packages", + "typings/env.d.ts" + ], "exclude": [ "node_modules", "**/dist",