-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imporve: imporve type infer of "createComponent"
Resolves: #15
- Loading branch information
Showing
13 changed files
with
256 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,27 @@ | ||
import { VueInstance } from './types/vue'; | ||
import { currentVue, getCurrentVue, getCurrentVM } from './runtimeContext'; | ||
import Vue, { ComponentOptions, VueConstructor } from 'vue'; | ||
import { ComponentInstance } from './ts-api'; | ||
import { currentVue, getCurrentVM } from './runtimeContext'; | ||
import { assert } from './utils'; | ||
|
||
export function ensureCurrentVMInFn(hook: string): VueInstance { | ||
export function ensureCurrentVMInFn(hook: string): ComponentInstance { | ||
const vm = getCurrentVM(); | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert(vm, `"${hook}" get called outside of "setup()"`); | ||
} | ||
return vm!; | ||
} | ||
|
||
export function compoundComputed(computed: { | ||
[key: string]: | ||
| (() => any) | ||
| { | ||
get?: () => any; | ||
set?: (v: any) => void; | ||
}; | ||
}) { | ||
const Vue = getCurrentVue(); | ||
const silent = Vue.config.silent; | ||
Vue.config.silent = true; | ||
const reactive = new Vue({ | ||
computed, | ||
}); | ||
Vue.config.silent = silent; | ||
return reactive; | ||
export function createComponentInstance<V extends Vue = Vue>( | ||
Ctor: VueConstructor<V>, | ||
options: ComponentOptions<V> = {} | ||
) { | ||
const silent = Ctor.config.silent; | ||
Ctor.config.silent = true; | ||
const vm = new Ctor(options); | ||
Ctor.config.silent = silent; | ||
return vm; | ||
} | ||
|
||
export function isVueInstance(obj: any) { | ||
export function isComponentInstance(obj: any) { | ||
return currentVue && obj instanceof currentVue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// import Vue, { VueConstructor, VNode, ComponentOptions as Vue2ComponentOptions } from 'vue'; | ||
import { VueConstructor, VNode, ComponentOptions as Vue2ComponentOptions } from 'vue'; | ||
import { ComponentPropsOptions, ExtractPropTypes } from './componentProps'; | ||
import { UnwrapValue } from '../wrappers'; | ||
|
||
export type Data = { [key: string]: unknown }; | ||
|
||
export type ComponentInstance = InstanceType<VueConstructor>; | ||
|
||
// public properties exposed on the proxy, which is used as the render context | ||
// in templates (as `this` in the render option) | ||
type ComponentRenderProxy<P = {}, S = {}, PublicProps = P> = { | ||
$data: S; | ||
$props: PublicProps; | ||
$attrs: Data; | ||
$refs: Data; | ||
$slots: Data; | ||
$root: ComponentInstance | null; | ||
$parent: ComponentInstance | null; | ||
$emit: (event: string, ...args: unknown[]) => void; | ||
} & P & | ||
S; | ||
|
||
// for Vetur and TSX support | ||
type VueConstructorProxy<PropsOptions, RawBindings> = { | ||
new (): ComponentRenderProxy< | ||
ExtractPropTypes<PropsOptions>, | ||
UnwrapValue<RawBindings>, | ||
ExtractPropTypes<PropsOptions, false> | ||
>; | ||
}; | ||
|
||
type VueProxy<PropsOptions, RawBindings> = Vue2ComponentOptions< | ||
never, | ||
UnwrapValue<RawBindings>, | ||
never, | ||
never, | ||
PropsOptions, | ||
ExtractPropTypes<PropsOptions, false> | ||
> & | ||
VueConstructorProxy<PropsOptions, RawBindings>; | ||
|
||
export interface SetupContext { | ||
readonly parent: ComponentInstance; | ||
readonly root: ComponentInstance; | ||
readonly refs: { [key: string]: ComponentInstance | Element | ComponentInstance[] | Element[] }; | ||
readonly slots: { [key: string]: VNode[] | undefined }; | ||
readonly attrs: Record<string, string>; | ||
|
||
emit(event: string, ...args: any[]): void; | ||
} | ||
|
||
type RenderFunction<Props> = (props: Props, ctx: SetupContext) => VNode; | ||
|
||
export type SetupFunction<Props, RawBindings> = ( | ||
this: void, | ||
props: Props, | ||
ctx: SetupContext | ||
) => RawBindings | RenderFunction<Props>; | ||
|
||
export interface ComponentOptions< | ||
PropsOptions = ComponentPropsOptions, | ||
RawBindings = Data, | ||
Props = ExtractPropTypes<PropsOptions> | ||
> { | ||
props?: PropsOptions; | ||
setup?: SetupFunction<Props, RawBindings>; | ||
} | ||
|
||
// object format with object props declaration | ||
// see `ExtractPropTypes` in ./componentProps.ts | ||
export function createComponent<PropsOptions, RawBindings>( | ||
options: ComponentOptions<PropsOptions, RawBindings> | ||
): VueProxy<PropsOptions, RawBindings>; | ||
// implementation, close to no-op | ||
export function createComponent(options: any) { | ||
return options as any; | ||
} |
Oops, something went wrong.