Skip to content

Commit 82a4805

Browse files
committed
wip: save
1 parent 40b279c commit 82a4805

File tree

8 files changed

+75
-87
lines changed

8 files changed

+75
-87
lines changed

src/core/util/next-tick.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ if (typeof Promise !== 'undefined' && isNative(Promise)) {
8686
}
8787

8888
export function nextTick(): Promise<void>
89-
export function nextTick(cb: (...args: any[]) => any, ctx?: object): void
89+
export function nextTick<T>(this: T, cb: (this: T, ...args: any[]) => any): void
90+
export function nextTick<T>(cb: (this: T, ...args: any[]) => any, ctx: T): void
9091
/**
9192
* @internal
9293
*/

types/common.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ export type HasDefined<T> = Equal<T, unknown> extends true ? false : true
1717
// If the the type T accepts type "any", output type Y, otherwise output type N.
1818
// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
1919
export type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N
20+
21+
export type LooseRequired<T> = { [P in string & keyof T]: T[P] }

types/options.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Vue, CreateElement, CombinedVueInstance } from './vue'
22
import { VNode, VNodeData, VNodeDirective, NormalizedScopedSlot } from './vnode'
33
import { SetupContext } from './v3-setup-context'
44
import { DebuggerEvent } from './v3-generated'
5+
import { DefineComponent } from './v3-define-component'
56

67
type Constructor = {
78
new (...args: any[]): any
@@ -201,6 +202,7 @@ export interface ComponentOptions<
201202
[key: string]:
202203
| Component<any, any, any, any>
203204
| AsyncComponent<any, any, any, any>
205+
| DefineComponent<any, any, any, any, any, any, any, any, any, any>
204206
}
205207
transitions?: { [key: string]: object }
206208
filters?: { [key: string]: Function }

types/test/v3/define-component-test.tsx

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import {
44
PropType,
55
ref,
66
reactive,
7-
ComponentPublicInstance,
8-
ComponentOptions,
9-
SetupContext,
10-
h
7+
ComponentPublicInstance
118
} from '../../index'
129
import { describe, test, expectType, expectError, IsUnion } from '../utils'
1310

@@ -297,15 +294,12 @@ describe('with object props', () => {
297294

298295
// @ts-expect-error missing required props
299296
expectError(<MyComponent />)
300-
301297
expectError(
302298
// @ts-expect-error wrong prop types
303299
<MyComponent a={'wrong type'} b="foo" dd={{ n: 1 }} ddd={['foo']} />
304300
)
305-
expectError(
306-
// @ts-expect-error wrong prop types
307-
<MyComponent ggg="baz" />
308-
)
301+
// @ts-expect-error wrong prop types
302+
expectError(<MyComponent ggg="baz" />)
309303
// @ts-expect-error
310304
expectError(<MyComponent b="foo" dd={{ n: 'string' }} ddd={['foo']} />)
311305
})
@@ -539,9 +533,9 @@ describe('with mixins', () => {
539533
setup(props) {
540534
expectType<string>(props.z)
541535
// props
542-
expectType<((...args: any[]) => any) | undefined>(props.onClick)
536+
// expectType<((...args: any[]) => any) | undefined>(props.onClick)
543537
// from Base
544-
expectType<((...args: any[]) => any) | undefined>(props.onBar)
538+
// expectType<((...args: any[]) => any) | undefined>(props.onBar)
545539
expectType<string>(props.aP1)
546540
expectType<boolean | undefined>(props.aP2)
547541
expectType<any>(props.bP1)
@@ -551,9 +545,9 @@ describe('with mixins', () => {
551545
render() {
552546
const props = this.$props
553547
// props
554-
expectType<((...args: any[]) => any) | undefined>(props.onClick)
548+
// expectType<((...args: any[]) => any) | undefined>(props.onClick)
555549
// from Base
556-
expectType<((...args: any[]) => any) | undefined>(props.onBar)
550+
// expectType<((...args: any[]) => any) | undefined>(props.onBar)
557551
expectType<string>(props.aP1)
558552
expectType<boolean | undefined>(props.aP2)
559553
expectType<any>(props.bP1)
@@ -737,11 +731,11 @@ describe('extends with mixins', () => {
737731
render() {
738732
const props = this.$props
739733
// props
740-
expectType<((...args: any[]) => any) | undefined>(props.onClick)
734+
// expectType<((...args: any[]) => any) | undefined>(props.onClick)
741735
// from Mixin
742-
expectType<((...args: any[]) => any) | undefined>(props.onBar)
736+
// expectType<((...args: any[]) => any) | undefined>(props.onBar)
743737
// from Base
744-
expectType<((...args: any[]) => any) | undefined>(props.onFoo)
738+
// expectType<((...args: any[]) => any) | undefined>(props.onFoo)
745739
expectType<boolean | undefined>(props.p1)
746740
expectType<number>(props.p2)
747741
expectType<string>(props.z)
@@ -851,8 +845,8 @@ describe('emits', () => {
851845
input: (b: string) => b.length > 1
852846
},
853847
setup(props, { emit }) {
854-
expectType<((n: number) => boolean) | undefined>(props.onClick)
855-
expectType<((b: string) => boolean) | undefined>(props.onInput)
848+
// expectType<((n: number) => boolean) | undefined>(props.onClick)
849+
// expectType<((b: string) => boolean) | undefined>(props.onInput)
856850
emit('click', 1)
857851
emit('input', 'foo')
858852
// @ts-expect-error
@@ -884,7 +878,6 @@ describe('emits', () => {
884878
// #3599
885879
this.$nextTick(function () {
886880
// this should be bound to this instance
887-
888881
this.$emit('click', 1)
889882
this.$emit('input', 'foo')
890883
// @ts-expect-error
@@ -905,8 +898,8 @@ describe('emits', () => {
905898
defineComponent({
906899
emits: ['foo', 'bar'],
907900
setup(props, { emit }) {
908-
expectType<((...args: any[]) => any) | undefined>(props.onFoo)
909-
expectType<((...args: any[]) => any) | undefined>(props.onBar)
901+
// expectType<((...args: any[]) => any) | undefined>(props.onFoo)
902+
// expectType<((...args: any[]) => any) | undefined>(props.onBar)
910903
emit('foo')
911904
emit('foo', 123)
912905
emit('bar')
@@ -928,7 +921,7 @@ describe('emits', () => {
928921
click: (n: number) => typeof n === 'number'
929922
},
930923
setup(props, { emit }) {
931-
expectType<((n: number) => any) | undefined>(props.onClick)
924+
// expectType<((n: number) => any) | undefined>(props.onClick)
932925
emit('click', 1)
933926
// @ts-expect-error
934927
expectError(emit('click'))
@@ -937,17 +930,17 @@ describe('emits', () => {
937930
}
938931
})
939932

940-
defineComponent({
941-
render() {
942-
return (
943-
<Component
944-
onClick={(n: number) => {
945-
return n + 1
946-
}}
947-
/>
948-
)
949-
}
950-
})
933+
// defineComponent({
934+
// render() {
935+
// return (
936+
// <Component
937+
// onClick={(n: number) => {
938+
// return n + 1
939+
// }}
940+
// />
941+
// )
942+
// }
943+
// })
951944

952945
// without emits
953946
defineComponent({
@@ -974,11 +967,11 @@ describe('emits', () => {
974967
})
975968
})
976969

977-
describe('componentOptions setup should be `SetupContext`', () => {
978-
expectType<ComponentOptions['setup']>(
979-
{} as (props: Record<string, any>, ctx: SetupContext) => any
980-
)
981-
})
970+
// describe('componentOptions setup should be `SetupContext`', () => {
971+
// expectType<ComponentOptions['setup']>(
972+
// {} as (props: Record<string, any>, ctx: SetupContext) => any
973+
// )
974+
// })
982975

983976
describe('extract instance type', () => {
984977
const Base = defineComponent({

types/v3-component-options.d.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Vue } from './vue'
22
import { VNode } from './vnode'
33
import { ComponentOptions as Vue2ComponentOptions } from './options'
44
import { EmitsOptions, SetupContext } from './v3-setup-context'
5-
import { Data } from './common'
5+
import { Data, LooseRequired, UnionToIntersection } from './common'
66
import { ComponentPropsOptions, ExtractPropTypes } from './v3-component-props'
77
import { CreateComponentPublicInstance } from './v3-component-public-instance'
88
export { ComponentPropsOptions } from './v3-component-props'
@@ -52,6 +52,21 @@ export type SetupFunction<
5252
ctx: SetupContext<Emits>
5353
) => RawBindings | (() => VNode | null) | void
5454

55+
type ExtractOptionProp<T> = T extends ComponentOptionsBase<
56+
infer P, // Props
57+
any, // RawBindings
58+
any, // D
59+
any, // C
60+
any, // M
61+
any, // Mixin
62+
any, // Extends
63+
any // EmitsOptions
64+
>
65+
? unknown extends P
66+
? {}
67+
: P
68+
: {}
69+
5570
export interface ComponentOptionsBase<
5671
Props,
5772
RawBindings,
@@ -67,9 +82,6 @@ export interface ComponentOptionsBase<
6782
'data' | 'computed' | 'method' | 'setup' | 'props' | 'mixins' | 'extends'
6883
>,
6984
ComponentCustomOptions {
70-
// allow any custom options
71-
[key: string]: any
72-
7385
// rewrite options api types
7486
data?: (
7587
this: CreateComponentPublicInstance<Props, {}, {}, {}, M, Mixin, Extends>,
@@ -79,7 +91,17 @@ export interface ComponentOptionsBase<
7991
mixins?: Mixin[]
8092
extends?: Extends
8193
emits?: (Emits | EmitNames[]) & ThisType<void>
82-
setup?: SetupFunction<Props, RawBindings, Emits>
94+
setup?: SetupFunction<
95+
Readonly<
96+
LooseRequired<
97+
Props &
98+
UnionToIntersection<ExtractOptionProp<Mixin>> &
99+
UnionToIntersection<ExtractOptionProp<Extends>>
100+
>
101+
>,
102+
RawBindings,
103+
Emits
104+
>
83105
}
84106

85107
export type ComponentOptionsMixin = ComponentOptionsBase<

types/v3-component-public-instance.d.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ import {
1717
ComponentOptionsMixin,
1818
ComponentOptionsBase
1919
} from './v3-component-options'
20-
import {
21-
ComponentRenderEmitFn,
22-
EmitFn,
23-
EmitsOptions,
24-
ObjectEmitsOptions,
25-
Slots
26-
} from './v3-setup-context'
20+
import { EmitFn, EmitsOptions, Slots } from './v3-setup-context'
2721

2822
/**
2923
* Custom properties added to component instances in any way and can be accessed through `this`
@@ -41,25 +35,6 @@ import {
4135
*/
4236
export interface ComponentCustomProperties {}
4337

44-
type EmitsToProps<T extends EmitsOptions> = T extends string[]
45-
? {
46-
[K in string & `on${Capitalize<T[number]>}`]?: (...args: any[]) => any
47-
}
48-
: T extends ObjectEmitsOptions
49-
? {
50-
[K in string &
51-
`on${Capitalize<string & keyof T>}`]?: K extends `on${infer C}`
52-
? T[Uncapitalize<C>] extends null
53-
? (...args: any[]) => any
54-
: (
55-
...args: T[Uncapitalize<C>] extends (...args: infer P) => any
56-
? P
57-
: never
58-
) => any
59-
: never
60-
}
61-
: {}
62-
6338
export type ComponentInstance = InstanceType<VueConstructor>
6439

6540
export type OptionTypesKeys = 'P' | 'B' | 'D' | 'C' | 'M' | 'Defaults'

types/v3-define-component.d.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import {
1515
} from './v3-component-options'
1616
import {
1717
ComponentPublicInstanceConstructor,
18-
ComponentPublicInstance,
19-
EmitsToProps,
2018
CreateComponentPublicInstance
2119
} from './v3-component-public-instance'
2220
import { Data, HasDefined } from './common'
@@ -36,8 +34,7 @@ type DefineComponent<
3634
PropsOrPropOptions extends ComponentPropsOptions
3735
? ExtractPropTypes<PropsOrPropOptions>
3836
: PropsOrPropOptions
39-
> &
40-
({} extends E ? {} : EmitsToProps<E>),
37+
>,
4138
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
4239
> = ComponentPublicInstanceConstructor<
4340
CreateComponentPublicInstance<
@@ -56,7 +53,9 @@ type DefineComponent<
5653
Props
5754
> &
5855
ComponentOptionsBase<Props, RawBindings, D, C, M, Mixin, Extends, E, EE> &
59-
Component
56+
Component & {
57+
props: PropsOrPropOptions
58+
}
6059

6160
/**
6261
* overload 1: object format with no props
@@ -66,8 +65,8 @@ export function defineComponent<
6665
D = Data,
6766
C extends ComputedOptions = {},
6867
M extends MethodOptions = {},
69-
Mixin = {},
70-
Extends = {},
68+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
69+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
7170
Emits extends EmitsOptions = {},
7271
EmitsNames extends string = string
7372
>(
@@ -96,8 +95,8 @@ export function defineComponent<
9695
D = Data,
9796
C extends ComputedOptions = {},
9897
M extends MethodOptions = {},
99-
Mixin = {},
100-
Extends = {},
98+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
99+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
101100
Emits extends EmitsOptions = {},
102101
EmitsNames extends string = string,
103102
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions
@@ -135,8 +134,8 @@ export function defineComponent<
135134
D = Data,
136135
C extends ComputedOptions = {},
137136
M extends MethodOptions = {},
138-
Mixin = {},
139-
Extends = {},
137+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
138+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
140139
Emits extends EmitsOptions = {},
141140
EmitsNames extends string = string,
142141
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions

types/v3-setup-context.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ export type EmitFn<
2929
}[Event]
3030
>
3131

32-
export type ComponentRenderEmitFn<
33-
Options = ObjectEmitsOptions,
34-
Event extends keyof Options = keyof Options,
35-
T extends Vue | void = void
36-
> = EmitFn<Options, Event, T>
37-
3832
export interface SetupContext<E extends EmitsOptions = {}> {
3933
attrs: Data
4034
slots: Slots

0 commit comments

Comments
 (0)