mirror of
https://github.com/element-plus/element-plus.git
synced 2025-08-26 12:32:17 +08:00
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
This commit is contained in:
@ -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')
|
||||
|
||||
|
@ -1,5 +1,3 @@
|
||||
import type { Prop } from 'vue'
|
||||
|
||||
export type Writable<T> = { -readonly [P in keyof T]: T[P] }
|
||||
export type WritableArray<T> = T extends readonly any[] ? Writable<T> : T
|
||||
|
||||
@ -9,55 +7,4 @@ export type IfUnknown<T, Y, N> = [unknown] extends [T] ? Y : N
|
||||
|
||||
export type UnknownToNever<T> = IfUnknown<T, never, T>
|
||||
|
||||
// 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<T, Y, N> = 0 extends 1 & T ? Y : N
|
||||
|
||||
type InferPropType<T, NullAsAny = true> = [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<string, any>
|
||||
: [T] extends [BooleanConstructor | { type: BooleanConstructor }]
|
||||
? boolean
|
||||
: [T] extends [DateConstructor | { type: DateConstructor }]
|
||||
? Date
|
||||
: [T] extends [(infer U)[] | { type: (infer U)[] }]
|
||||
? U extends DateConstructor
|
||||
? Date | InferPropType<U, false>
|
||||
: InferPropType<U, false>
|
||||
: [T] extends [Prop<infer V, infer D>]
|
||||
? unknown extends V
|
||||
? keyof V extends never
|
||||
? IfAny<V, V, D>
|
||||
: V
|
||||
: V
|
||||
: T
|
||||
|
||||
type PublicRequiredKeys<T> = {
|
||||
[K in keyof T]: T[K] extends { required: true } ? K : never
|
||||
}[keyof T]
|
||||
|
||||
type PublicOptionalKeys<T> = Exclude<keyof T, PublicRequiredKeys<T>>
|
||||
|
||||
/**
|
||||
* 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<O> = {
|
||||
[K in keyof Pick<O, PublicRequiredKeys<O>>]: InferPropType<O[K]>
|
||||
} & {
|
||||
[K in keyof Pick<O, PublicOptionalKeys<O>>]?: InferPropType<O[K]>
|
||||
}
|
||||
}
|
||||
// delete when upgrade to vue 3.3 : end
|
||||
export {}
|
||||
|
51
packages/utils/vue3.3.polyfill.ts
Normal file
51
packages/utils/vue3.3.polyfill.ts
Normal file
@ -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<T, Y, N> = 0 extends 1 & T ? Y : N
|
||||
|
||||
type InferPropType<T, NullAsAny = true> = [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<string, any>
|
||||
: [T] extends [BooleanConstructor | { type: BooleanConstructor }]
|
||||
? boolean
|
||||
: [T] extends [DateConstructor | { type: DateConstructor }]
|
||||
? Date
|
||||
: [T] extends [(infer U)[] | { type: (infer U)[] }]
|
||||
? U extends DateConstructor
|
||||
? Date | InferPropType<U, false>
|
||||
: InferPropType<U, false>
|
||||
: [T] extends [Prop<infer V, infer D>]
|
||||
? unknown extends V
|
||||
? keyof V extends never
|
||||
? IfAny<V, V, D>
|
||||
: V
|
||||
: V
|
||||
: T
|
||||
|
||||
type PublicRequiredKeys<T> = {
|
||||
[K in keyof T]: T[K] extends { required: true } ? K : never
|
||||
}[keyof T]
|
||||
|
||||
type PublicOptionalKeys<T> = Exclude<keyof T, PublicRequiredKeys<T>>
|
||||
|
||||
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<O> = {
|
||||
[K in keyof Pick<O, PublicRequiredKeys<O>>]: InferPropType<O[K]>
|
||||
} & {
|
||||
[K in keyof Pick<O, PublicOptionalKeys<O>>]?: InferPropType<O[K]>
|
||||
}
|
||||
}
|
||||
// delete when upgrade to vue 3.3 : end
|
@ -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",
|
||||
|
@ -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"]
|
||||
}
|
||||
|
@ -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",
|
||||
|
Reference in New Issue
Block a user