mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[improvement] enable tsx type checking for functional component (#2764)
This commit is contained in:
parent
2ad525c45d
commit
af0fb98c9e
@ -3,7 +3,7 @@ module.exports = {
|
|||||||
setupFiles: ['<rootDir>/test/jest.init.js'],
|
setupFiles: ['<rootDir>/test/jest.init.js'],
|
||||||
moduleFileExtensions: ['js', 'vue', 'ts', 'tsx'],
|
moduleFileExtensions: ['js', 'vue', 'ts', 'tsx'],
|
||||||
transform: {
|
transform: {
|
||||||
'^.+\\.js$': '<rootDir>/test/jest.transform.js',
|
'^.+\\.(js|ts|tsx)$': '<rootDir>/test/jest.transform.js',
|
||||||
'.*\\.(vue)$': '<rootDir>/node_modules/vue-jest'
|
'.*\\.(vue)$': '<rootDir>/node_modules/vue-jest'
|
||||||
},
|
},
|
||||||
moduleNameMapper: {
|
moduleNameMapper: {
|
||||||
|
@ -21,7 +21,7 @@ const inheritKey = [
|
|||||||
const mapInheritKey: ObjectIndex = { nativeOn: 'on' };
|
const mapInheritKey: ObjectIndex = { nativeOn: 'on' };
|
||||||
|
|
||||||
// inherit partial context, map nativeOn to on
|
// inherit partial context, map nativeOn to on
|
||||||
export function inherit(context: Context, inheritListeners: boolean): InheritContext {
|
export function inherit(context: Context, inheritListeners?: boolean): InheritContext {
|
||||||
const result = inheritKey.reduce(
|
const result = inheritKey.reduce(
|
||||||
(obj, key) => {
|
(obj, key) => {
|
||||||
if (context.data[key]) {
|
if (context.data[key]) {
|
||||||
|
@ -10,25 +10,27 @@ import Vue, {
|
|||||||
CreateElement,
|
CreateElement,
|
||||||
RenderContext
|
RenderContext
|
||||||
} from 'vue/types';
|
} from 'vue/types';
|
||||||
import { VNode, ScopedSlot } from 'vue/types/vnode';
|
import { VNode } from 'vue/types/vnode';
|
||||||
import { InjectOptions, PropsDefinition } from 'vue/types/options';
|
import { InjectOptions, PropsDefinition } from 'vue/types/options';
|
||||||
|
|
||||||
type VantComponentOptions = ComponentOptions<Vue> & {
|
export type ScopedSlot = (props?: any) => VNode[] | undefined;
|
||||||
|
|
||||||
|
export type VantComponentOptions = ComponentOptions<Vue> & {
|
||||||
functional?: boolean;
|
functional?: boolean;
|
||||||
install?: (Vue: VueConstructor) => void;
|
install?: (Vue: VueConstructor) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
type DefaultProps = Record<string, any>;
|
export type DefaultProps = Record<string, any>;
|
||||||
|
|
||||||
type VantPureComponent<
|
export type FunctionalComponent<
|
||||||
Props = DefaultProps,
|
Props = DefaultProps,
|
||||||
PropDefs = PropsDefinition<Props>
|
PropDefs = PropsDefinition<Props>
|
||||||
> = {
|
> = {
|
||||||
(
|
(
|
||||||
h: CreateElement,
|
h: CreateElement,
|
||||||
props: { [key: string]: any },
|
props: Props,
|
||||||
slots: { [key: string]: ScopedSlot | undefined },
|
slots: { [key: string]: ScopedSlot | undefined },
|
||||||
context: RenderContext
|
context: RenderContext<Props>
|
||||||
): VNode;
|
): VNode;
|
||||||
props?: PropDefs;
|
props?: PropDefs;
|
||||||
model?: {
|
model?: {
|
||||||
@ -38,6 +40,8 @@ type VantPureComponent<
|
|||||||
inject?: InjectOptions;
|
inject?: InjectOptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type VantTsxComponent<T> = (props: T) => VNode;
|
||||||
|
|
||||||
const arrayProp = {
|
const arrayProp = {
|
||||||
type: Array,
|
type: Array,
|
||||||
default: () => []
|
default: () => []
|
||||||
@ -80,7 +84,9 @@ export function unifySlots(context: RenderContext) {
|
|||||||
return scopedSlots;
|
return scopedSlots;
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformPureComponent(pure: VantPureComponent): VantComponentOptions {
|
function transformFunctionalComponent(
|
||||||
|
pure: FunctionalComponent
|
||||||
|
): VantComponentOptions {
|
||||||
return {
|
return {
|
||||||
functional: true,
|
functional: true,
|
||||||
props: pure.props,
|
props: pure.props,
|
||||||
@ -89,11 +95,11 @@ function transformPureComponent(pure: VantPureComponent): VantComponentOptions {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default (name: string) => (
|
export default (name: string) => <T = DefaultProps>(
|
||||||
sfc: VantComponentOptions | VantPureComponent
|
sfc: VantComponentOptions | FunctionalComponent
|
||||||
) => {
|
): VantTsxComponent<T> => {
|
||||||
if (typeof sfc === 'function') {
|
if (typeof sfc === 'function') {
|
||||||
sfc = transformPureComponent(sfc);
|
sfc = transformFunctionalComponent(sfc);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sfc.functional) {
|
if (!sfc.functional) {
|
||||||
@ -108,5 +114,5 @@ export default (name: string) => (
|
|||||||
sfc.name = name;
|
sfc.name = name;
|
||||||
sfc.install = install;
|
sfc.install = install;
|
||||||
|
|
||||||
return sfc;
|
return sfc as VantTsxComponent<T>;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user