2020-02-20 01:29:01 +08:00
|
|
|
import {
|
|
|
|
describe,
|
2020-08-20 04:11:29 +08:00
|
|
|
Component,
|
2020-02-20 01:29:01 +08:00
|
|
|
defineComponent,
|
|
|
|
PropType,
|
|
|
|
ref,
|
|
|
|
reactive,
|
2020-06-09 22:17:42 +08:00
|
|
|
createApp,
|
|
|
|
expectError,
|
2020-07-08 23:51:03 +08:00
|
|
|
expectType,
|
2020-07-15 21:19:20 +08:00
|
|
|
ComponentPublicInstance,
|
|
|
|
ComponentOptions,
|
|
|
|
SetupContext
|
2020-02-20 01:29:01 +08:00
|
|
|
} from './index'
|
2019-11-05 07:38:55 +08:00
|
|
|
|
|
|
|
describe('with object props', () => {
|
|
|
|
interface ExpectedProps {
|
|
|
|
a?: number | undefined
|
|
|
|
b: string
|
2020-04-03 21:28:13 +08:00
|
|
|
e?: Function
|
2019-11-05 07:38:55 +08:00
|
|
|
bb: string
|
2020-08-18 23:37:34 +08:00
|
|
|
bbb: string
|
2019-11-05 07:38:55 +08:00
|
|
|
cc?: string[] | undefined
|
2020-04-03 21:29:21 +08:00
|
|
|
dd: { n: 1 }
|
2020-04-03 21:28:13 +08:00
|
|
|
ee?: () => string
|
|
|
|
ff?: (a: number, b: string) => { a: boolean }
|
2019-11-05 07:38:55 +08:00
|
|
|
ccc?: string[] | undefined
|
|
|
|
ddd: string[]
|
2020-04-03 21:28:13 +08:00
|
|
|
eee: () => { a: string }
|
|
|
|
fff: (a: number, b: string) => { a: boolean }
|
2020-06-09 22:37:00 +08:00
|
|
|
hhh: boolean
|
2020-08-18 23:23:18 +08:00
|
|
|
ggg: 'foo' | 'bar'
|
2020-08-20 04:36:42 +08:00
|
|
|
ffff: (a: number, b: string) => { a: boolean }
|
2020-08-15 05:40:41 +08:00
|
|
|
validated?: string
|
2019-11-05 07:38:55 +08:00
|
|
|
}
|
|
|
|
|
2020-01-17 06:47:47 +08:00
|
|
|
type GT = string & { __brand: unknown }
|
|
|
|
|
2019-12-22 23:58:12 +08:00
|
|
|
const MyComponent = defineComponent({
|
2019-11-05 07:38:55 +08:00
|
|
|
props: {
|
|
|
|
a: Number,
|
|
|
|
// required should make property non-void
|
|
|
|
b: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
},
|
2020-04-03 21:28:13 +08:00
|
|
|
e: Function,
|
2019-11-05 07:38:55 +08:00
|
|
|
// default value should infer type and make it non-void
|
|
|
|
bb: {
|
|
|
|
default: 'hello'
|
|
|
|
},
|
2020-08-18 23:37:34 +08:00
|
|
|
bbb: {
|
|
|
|
// Note: default function value requires arrow syntax + explicit
|
|
|
|
// annotation
|
|
|
|
default: (props: any) => (props.bb as string) || 'foo'
|
|
|
|
},
|
2019-11-05 07:38:55 +08:00
|
|
|
// explicit type casting
|
|
|
|
cc: Array as PropType<string[]>,
|
|
|
|
// required + type casting
|
|
|
|
dd: {
|
2020-04-03 21:29:21 +08:00
|
|
|
type: Object as PropType<{ n: 1 }>,
|
2019-11-05 07:38:55 +08:00
|
|
|
required: true
|
|
|
|
},
|
2020-04-03 21:28:13 +08:00
|
|
|
// return type
|
|
|
|
ee: Function as PropType<() => string>,
|
|
|
|
// arguments + object return
|
|
|
|
ff: Function as PropType<(a: number, b: string) => { a: boolean }>,
|
2019-11-05 07:38:55 +08:00
|
|
|
// explicit type casting with constructor
|
|
|
|
ccc: Array as () => string[],
|
|
|
|
// required + contructor type casting
|
|
|
|
ddd: {
|
|
|
|
type: Array as () => string[],
|
|
|
|
required: true
|
2020-04-03 21:28:13 +08:00
|
|
|
},
|
|
|
|
// required + object return
|
|
|
|
eee: {
|
|
|
|
type: Function as PropType<() => { a: string }>,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
// required + arguments + object return
|
|
|
|
fff: {
|
|
|
|
type: Function as PropType<(a: number, b: string) => { a: boolean }>,
|
|
|
|
required: true
|
2020-06-09 22:37:00 +08:00
|
|
|
},
|
|
|
|
hhh: {
|
|
|
|
type: Boolean,
|
|
|
|
required: true
|
2020-08-15 05:40:41 +08:00
|
|
|
},
|
2020-08-18 23:23:18 +08:00
|
|
|
// default + type casting
|
|
|
|
ggg: {
|
|
|
|
type: String as PropType<'foo' | 'bar'>,
|
|
|
|
default: 'foo'
|
|
|
|
},
|
2020-08-20 04:36:42 +08:00
|
|
|
// default + function
|
|
|
|
ffff: {
|
|
|
|
type: Function as PropType<(a: number, b: string) => { a: boolean }>,
|
2020-08-25 09:37:22 +08:00
|
|
|
default: (a: number, b: string) => ({ a: a > +b })
|
2020-08-20 04:36:42 +08:00
|
|
|
},
|
2020-08-15 05:40:41 +08:00
|
|
|
validated: {
|
|
|
|
type: String,
|
2020-08-18 23:37:34 +08:00
|
|
|
// validator requires explicit annotation
|
2020-08-15 05:40:41 +08:00
|
|
|
validator: (val: unknown) => val !== ''
|
2019-11-05 07:38:55 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setup(props) {
|
|
|
|
// type assertion. See https://github.com/SamVerschueren/tsd
|
|
|
|
expectType<ExpectedProps['a']>(props.a)
|
|
|
|
expectType<ExpectedProps['b']>(props.b)
|
2020-04-03 21:28:13 +08:00
|
|
|
expectType<ExpectedProps['e']>(props.e)
|
2019-11-05 07:38:55 +08:00
|
|
|
expectType<ExpectedProps['bb']>(props.bb)
|
2020-08-18 23:37:34 +08:00
|
|
|
expectType<ExpectedProps['bbb']>(props.bbb)
|
2019-11-05 07:38:55 +08:00
|
|
|
expectType<ExpectedProps['cc']>(props.cc)
|
|
|
|
expectType<ExpectedProps['dd']>(props.dd)
|
2020-04-03 21:28:13 +08:00
|
|
|
expectType<ExpectedProps['ee']>(props.ee)
|
|
|
|
expectType<ExpectedProps['ff']>(props.ff)
|
2019-11-05 07:38:55 +08:00
|
|
|
expectType<ExpectedProps['ccc']>(props.ccc)
|
|
|
|
expectType<ExpectedProps['ddd']>(props.ddd)
|
2020-04-03 21:28:13 +08:00
|
|
|
expectType<ExpectedProps['eee']>(props.eee)
|
|
|
|
expectType<ExpectedProps['fff']>(props.fff)
|
2020-06-09 22:37:00 +08:00
|
|
|
expectType<ExpectedProps['hhh']>(props.hhh)
|
2020-08-18 23:23:18 +08:00
|
|
|
expectType<ExpectedProps['ggg']>(props.ggg)
|
2020-08-20 04:36:42 +08:00
|
|
|
expectType<ExpectedProps['ffff']>(props.ffff)
|
2020-08-15 05:40:41 +08:00
|
|
|
expectType<ExpectedProps['validated']>(props.validated)
|
2019-11-05 07:38:55 +08:00
|
|
|
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error props should be readonly
|
2019-11-09 23:03:58 +08:00
|
|
|
expectError((props.a = 1))
|
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
// setup context
|
|
|
|
return {
|
|
|
|
c: ref(1),
|
2020-02-27 08:01:42 +08:00
|
|
|
d: {
|
2019-11-05 07:38:55 +08:00
|
|
|
e: ref('hi')
|
2020-02-27 08:01:42 +08:00
|
|
|
},
|
2020-02-20 01:29:01 +08:00
|
|
|
f: reactive({
|
2020-01-17 06:47:47 +08:00
|
|
|
g: ref('hello' as GT)
|
2020-02-27 08:01:42 +08:00
|
|
|
})
|
2019-11-05 07:38:55 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
const props = this.$props
|
|
|
|
expectType<ExpectedProps['a']>(props.a)
|
|
|
|
expectType<ExpectedProps['b']>(props.b)
|
2020-04-03 21:28:13 +08:00
|
|
|
expectType<ExpectedProps['e']>(props.e)
|
2019-11-05 07:38:55 +08:00
|
|
|
expectType<ExpectedProps['bb']>(props.bb)
|
|
|
|
expectType<ExpectedProps['cc']>(props.cc)
|
|
|
|
expectType<ExpectedProps['dd']>(props.dd)
|
2020-04-03 21:28:13 +08:00
|
|
|
expectType<ExpectedProps['ee']>(props.ee)
|
|
|
|
expectType<ExpectedProps['ff']>(props.ff)
|
2019-11-05 07:38:55 +08:00
|
|
|
expectType<ExpectedProps['ccc']>(props.ccc)
|
|
|
|
expectType<ExpectedProps['ddd']>(props.ddd)
|
2020-04-03 21:28:13 +08:00
|
|
|
expectType<ExpectedProps['eee']>(props.eee)
|
|
|
|
expectType<ExpectedProps['fff']>(props.fff)
|
2020-06-09 22:37:00 +08:00
|
|
|
expectType<ExpectedProps['hhh']>(props.hhh)
|
2020-08-18 23:23:18 +08:00
|
|
|
expectType<ExpectedProps['ggg']>(props.ggg)
|
2019-11-05 07:38:55 +08:00
|
|
|
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error props should be readonly
|
2019-11-10 07:40:25 +08:00
|
|
|
expectError((props.a = 1))
|
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
// should also expose declared props on `this`
|
|
|
|
expectType<ExpectedProps['a']>(this.a)
|
|
|
|
expectType<ExpectedProps['b']>(this.b)
|
2020-04-03 21:28:13 +08:00
|
|
|
expectType<ExpectedProps['e']>(this.e)
|
2019-11-05 07:38:55 +08:00
|
|
|
expectType<ExpectedProps['bb']>(this.bb)
|
|
|
|
expectType<ExpectedProps['cc']>(this.cc)
|
|
|
|
expectType<ExpectedProps['dd']>(this.dd)
|
2020-04-03 21:28:13 +08:00
|
|
|
expectType<ExpectedProps['ee']>(this.ee)
|
|
|
|
expectType<ExpectedProps['ff']>(this.ff)
|
2019-11-05 07:38:55 +08:00
|
|
|
expectType<ExpectedProps['ccc']>(this.ccc)
|
|
|
|
expectType<ExpectedProps['ddd']>(this.ddd)
|
2020-04-03 21:28:13 +08:00
|
|
|
expectType<ExpectedProps['eee']>(this.eee)
|
|
|
|
expectType<ExpectedProps['fff']>(this.fff)
|
2020-06-09 22:37:00 +08:00
|
|
|
expectType<ExpectedProps['hhh']>(this.hhh)
|
2020-08-18 23:23:18 +08:00
|
|
|
expectType<ExpectedProps['ggg']>(this.ggg)
|
2019-11-05 07:38:55 +08:00
|
|
|
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error props on `this` should be readonly
|
2019-11-10 07:40:25 +08:00
|
|
|
expectError((this.a = 1))
|
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
// assert setup context unwrapping
|
|
|
|
expectType<number>(this.c)
|
2020-07-29 04:30:56 +08:00
|
|
|
expectType<string>(this.d.e.value)
|
2020-01-17 06:47:47 +08:00
|
|
|
expectType<GT>(this.f.g)
|
2019-11-05 07:38:55 +08:00
|
|
|
|
2019-11-10 07:40:25 +08:00
|
|
|
// setup context properties should be mutable
|
|
|
|
this.c = 2
|
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-20 04:11:29 +08:00
|
|
|
expectType<Component>(MyComponent)
|
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
// Test TSX
|
|
|
|
expectType<JSX.Element>(
|
|
|
|
<MyComponent
|
|
|
|
a={1}
|
|
|
|
b="b"
|
|
|
|
bb="bb"
|
2020-04-03 21:28:13 +08:00
|
|
|
e={() => {}}
|
2019-11-05 07:38:55 +08:00
|
|
|
cc={['cc']}
|
2020-04-03 21:29:21 +08:00
|
|
|
dd={{ n: 1 }}
|
2020-04-03 21:28:13 +08:00
|
|
|
ee={() => 'ee'}
|
2019-11-05 07:38:55 +08:00
|
|
|
ccc={['ccc']}
|
|
|
|
ddd={['ddd']}
|
2020-04-03 21:28:13 +08:00
|
|
|
eee={() => ({ a: 'eee' })}
|
|
|
|
fff={(a, b) => ({ a: a > +b })}
|
2020-06-09 22:37:00 +08:00
|
|
|
hhh={false}
|
2020-08-18 23:23:18 +08:00
|
|
|
ggg="foo"
|
2020-07-17 23:43:28 +08:00
|
|
|
// should allow class/style as attrs
|
2019-11-05 07:38:55 +08:00
|
|
|
class="bar"
|
2020-07-17 23:43:28 +08:00
|
|
|
style={{ color: 'red' }}
|
2019-11-05 07:38:55 +08:00
|
|
|
// should allow key
|
|
|
|
key={'foo'}
|
|
|
|
// should allow ref
|
|
|
|
ref={'foo'}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
2020-08-20 04:11:29 +08:00
|
|
|
expectType<Component>(
|
|
|
|
<MyComponent
|
|
|
|
b="b"
|
|
|
|
dd={{ n: 1 }}
|
|
|
|
ddd={['ddd']}
|
|
|
|
eee={() => ({ a: 'eee' })}
|
|
|
|
fff={(a, b) => ({ a: a > +b })}
|
|
|
|
hhh={false}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error missing required props
|
2019-11-05 07:38:55 +08:00
|
|
|
expectError(<MyComponent />)
|
|
|
|
|
|
|
|
expectError(
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error wrong prop types
|
2020-04-03 22:51:17 +08:00
|
|
|
<MyComponent a={'wrong type'} b="foo" dd={{ n: 1 }} ddd={['foo']} />
|
2019-11-05 07:38:55 +08:00
|
|
|
)
|
2020-08-18 23:23:18 +08:00
|
|
|
expectError(
|
|
|
|
// @ts-expect-error wrong prop types
|
|
|
|
<MyComponent ggg="baz" />
|
|
|
|
)
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-03 22:51:17 +08:00
|
|
|
expectError(<MyComponent b="foo" dd={{ n: 'string' }} ddd={['foo']} />)
|
2020-08-18 23:25:55 +08:00
|
|
|
|
|
|
|
// `this` should be void inside of prop validators and prop default factories
|
|
|
|
defineComponent({
|
|
|
|
props: {
|
|
|
|
myProp: {
|
|
|
|
type: Number,
|
|
|
|
validator(val: unknown): boolean {
|
|
|
|
// @ts-expect-error
|
|
|
|
return val !== this.otherProp
|
|
|
|
},
|
|
|
|
default(): number {
|
|
|
|
// @ts-expect-error
|
|
|
|
return this.otherProp + 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
otherProp: {
|
|
|
|
type: Number,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-11-05 07:38:55 +08:00
|
|
|
})
|
|
|
|
|
2020-04-04 00:05:52 +08:00
|
|
|
// describe('type inference w/ optional props declaration', () => {
|
|
|
|
// const MyComponent = defineComponent({
|
|
|
|
// setup(_props: { msg: string }) {
|
|
|
|
// return {
|
|
|
|
// a: 1
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// render() {
|
|
|
|
// expectType<string>(this.$props.msg)
|
|
|
|
// // props should be readonly
|
|
|
|
// expectError((this.$props.msg = 'foo'))
|
|
|
|
// // should not expose on `this`
|
|
|
|
// expectError(this.msg)
|
|
|
|
// expectType<number>(this.a)
|
|
|
|
// return null
|
|
|
|
// }
|
|
|
|
// })
|
2019-11-05 07:38:55 +08:00
|
|
|
|
2020-04-04 00:05:52 +08:00
|
|
|
// expectType<JSX.Element>(<MyComponent msg="foo" />)
|
|
|
|
// expectError(<MyComponent />)
|
|
|
|
// expectError(<MyComponent msg={1} />)
|
|
|
|
// })
|
2019-11-05 07:38:55 +08:00
|
|
|
|
2020-04-04 00:05:52 +08:00
|
|
|
// describe('type inference w/ direct setup function', () => {
|
|
|
|
// const MyComponent = defineComponent((_props: { msg: string }) => {})
|
|
|
|
// expectType<JSX.Element>(<MyComponent msg="foo" />)
|
|
|
|
// expectError(<MyComponent />)
|
|
|
|
// expectError(<MyComponent msg={1} />)
|
|
|
|
// })
|
2019-11-05 07:38:55 +08:00
|
|
|
|
|
|
|
describe('type inference w/ array props declaration', () => {
|
2020-06-09 22:37:00 +08:00
|
|
|
const MyComponent = defineComponent({
|
2019-11-05 07:38:55 +08:00
|
|
|
props: ['a', 'b'],
|
|
|
|
setup(props) {
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error props should be readonly
|
2019-11-10 07:40:25 +08:00
|
|
|
expectError((props.a = 1))
|
|
|
|
expectType<any>(props.a)
|
|
|
|
expectType<any>(props.b)
|
2019-11-05 07:38:55 +08:00
|
|
|
return {
|
|
|
|
c: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
2019-11-10 07:40:25 +08:00
|
|
|
expectType<any>(this.$props.a)
|
|
|
|
expectType<any>(this.$props.b)
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2019-11-10 07:40:25 +08:00
|
|
|
expectError((this.$props.a = 1))
|
2019-11-05 07:38:55 +08:00
|
|
|
expectType<any>(this.a)
|
|
|
|
expectType<any>(this.b)
|
|
|
|
expectType<number>(this.c)
|
|
|
|
}
|
|
|
|
})
|
2020-06-09 22:37:00 +08:00
|
|
|
expectType<JSX.Element>(<MyComponent a={[1, 2]} b="b" />)
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<MyComponent other="other" />)
|
2019-11-05 07:38:55 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('type inference w/ options API', () => {
|
2019-12-22 23:58:12 +08:00
|
|
|
defineComponent({
|
2019-11-05 07:38:55 +08:00
|
|
|
props: { a: Number },
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
b: 123
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
// Limitation: we cannot expose the return result of setup() on `this`
|
|
|
|
// here in data() - somehow that would mess up the inference
|
|
|
|
expectType<number | undefined>(this.a)
|
|
|
|
return {
|
|
|
|
c: this.a || 123
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
d(): number {
|
|
|
|
expectType<number>(this.b)
|
|
|
|
return this.b + 1
|
2020-07-29 03:40:23 +08:00
|
|
|
},
|
|
|
|
e: {
|
|
|
|
get(): number {
|
|
|
|
expectType<number>(this.b)
|
|
|
|
expectType<number>(this.d)
|
|
|
|
|
|
|
|
return this.b + this.d
|
|
|
|
},
|
|
|
|
set(v: number) {
|
|
|
|
expectType<number>(this.b)
|
|
|
|
expectType<number>(this.d)
|
|
|
|
expectType<number>(v)
|
|
|
|
}
|
2019-11-05 07:38:55 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
a() {
|
|
|
|
expectType<number>(this.b)
|
|
|
|
this.b + 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
// props
|
|
|
|
expectType<number | undefined>(this.a)
|
|
|
|
// returned from setup()
|
|
|
|
expectType<number>(this.b)
|
|
|
|
// returned from data()
|
|
|
|
expectType<number>(this.c)
|
|
|
|
// computed
|
|
|
|
expectType<number>(this.d)
|
2020-07-29 03:40:23 +08:00
|
|
|
// computed get/set
|
|
|
|
expectType<number>(this.e)
|
2019-11-05 07:38:55 +08:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
doSomething() {
|
|
|
|
// props
|
|
|
|
expectType<number | undefined>(this.a)
|
|
|
|
// returned from setup()
|
|
|
|
expectType<number>(this.b)
|
|
|
|
// returned from data()
|
|
|
|
expectType<number>(this.c)
|
|
|
|
// computed
|
|
|
|
expectType<number>(this.d)
|
2020-07-29 03:40:23 +08:00
|
|
|
// computed get/set
|
|
|
|
expectType<number>(this.e)
|
2020-07-29 03:31:23 +08:00
|
|
|
},
|
|
|
|
returnSomething() {
|
|
|
|
return this.a
|
2019-11-05 07:38:55 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
// props
|
|
|
|
expectType<number | undefined>(this.a)
|
|
|
|
// returned from setup()
|
|
|
|
expectType<number>(this.b)
|
|
|
|
// returned from data()
|
|
|
|
expectType<number>(this.c)
|
|
|
|
// computed
|
|
|
|
expectType<number>(this.d)
|
2020-07-29 03:40:23 +08:00
|
|
|
// computed get/set
|
|
|
|
expectType<number>(this.e)
|
2020-07-29 03:31:23 +08:00
|
|
|
// method
|
|
|
|
expectType<() => number | undefined>(this.returnSomething)
|
2019-11-05 07:38:55 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-12-25 00:04:35 +08:00
|
|
|
|
2020-06-09 22:37:00 +08:00
|
|
|
describe('with mixins', () => {
|
|
|
|
const MixinA = defineComponent({
|
|
|
|
props: {
|
|
|
|
aP1: {
|
|
|
|
type: String,
|
|
|
|
default: 'aP1'
|
|
|
|
},
|
|
|
|
aP2: Boolean
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
a: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const MixinB = defineComponent({
|
|
|
|
props: ['bP1', 'bP2'],
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
b: 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const MixinC = defineComponent({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
c: 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const MixinD = defineComponent({
|
|
|
|
mixins: [MixinA],
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
d: 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
dC1(): number {
|
|
|
|
return this.d + this.a
|
|
|
|
},
|
|
|
|
dC2(): string {
|
|
|
|
return this.aP1 + 'dC2'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const MyComponent = defineComponent({
|
|
|
|
mixins: [MixinA, MixinB, MixinC, MixinD],
|
|
|
|
props: {
|
|
|
|
// required should make property non-void
|
|
|
|
z: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
const props = this.$props
|
|
|
|
// props
|
|
|
|
expectType<string>(props.aP1)
|
|
|
|
expectType<boolean | undefined>(props.aP2)
|
|
|
|
expectType<any>(props.bP1)
|
|
|
|
expectType<any>(props.bP2)
|
|
|
|
expectType<string>(props.z)
|
|
|
|
|
|
|
|
const data = this.$data
|
|
|
|
expectType<number>(data.a)
|
|
|
|
expectType<number>(data.b)
|
|
|
|
expectType<number>(data.c)
|
|
|
|
expectType<number>(data.d)
|
|
|
|
|
|
|
|
// should also expose declared props on `this`
|
|
|
|
expectType<number>(this.a)
|
|
|
|
expectType<string>(this.aP1)
|
|
|
|
expectType<boolean | undefined>(this.aP2)
|
|
|
|
expectType<number>(this.b)
|
|
|
|
expectType<any>(this.bP1)
|
|
|
|
expectType<number>(this.c)
|
|
|
|
expectType<number>(this.d)
|
|
|
|
expectType<number>(this.dC1)
|
|
|
|
expectType<string>(this.dC2)
|
|
|
|
|
|
|
|
// props should be readonly
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError((this.aP1 = 'new'))
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError((this.z = 1))
|
|
|
|
|
|
|
|
// props on `this` should be readonly
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError((this.bP1 = 1))
|
|
|
|
|
|
|
|
// string value can not assigned to number type value
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError((this.c = '1'))
|
|
|
|
|
|
|
|
// setup context properties should be mutable
|
|
|
|
this.d = 5
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Test TSX
|
|
|
|
expectType<JSX.Element>(
|
|
|
|
<MyComponent aP1={'aP'} aP2 bP1={1} bP2={[1, 2]} z={'z'} />
|
|
|
|
)
|
|
|
|
|
|
|
|
// missing required props
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<MyComponent />)
|
|
|
|
|
|
|
|
// wrong prop types
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<MyComponent aP1="ap" aP2={'wrong type'} bP1="b" z={'z'} />)
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<MyComponent aP1={1} bP2={[1]} />)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with extends', () => {
|
|
|
|
const Base = defineComponent({
|
|
|
|
props: {
|
|
|
|
aP1: Boolean,
|
|
|
|
aP2: {
|
|
|
|
type: Number,
|
|
|
|
default: 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
a: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
c(): number {
|
|
|
|
return this.aP2 + this.a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const MyComponent = defineComponent({
|
|
|
|
extends: Base,
|
|
|
|
props: {
|
|
|
|
// required should make property non-void
|
|
|
|
z: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
const props = this.$props
|
|
|
|
// props
|
|
|
|
expectType<boolean | undefined>(props.aP1)
|
|
|
|
expectType<number>(props.aP2)
|
|
|
|
expectType<string>(props.z)
|
|
|
|
|
|
|
|
const data = this.$data
|
|
|
|
expectType<number>(data.a)
|
|
|
|
|
|
|
|
// should also expose declared props on `this`
|
|
|
|
expectType<number>(this.a)
|
|
|
|
expectType<boolean | undefined>(this.aP1)
|
|
|
|
expectType<number>(this.aP2)
|
|
|
|
|
|
|
|
// setup context properties should be mutable
|
|
|
|
this.a = 5
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Test TSX
|
|
|
|
expectType<JSX.Element>(<MyComponent aP2={3} aP1 z={'z'} />)
|
|
|
|
|
|
|
|
// missing required props
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<MyComponent />)
|
|
|
|
|
|
|
|
// wrong prop types
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<MyComponent aP2={'wrong type'} z={'z'} />)
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<MyComponent aP1={3} />)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('extends with mixins', () => {
|
|
|
|
const Mixin = defineComponent({
|
|
|
|
props: {
|
|
|
|
mP1: {
|
|
|
|
type: String,
|
|
|
|
default: 'mP1'
|
|
|
|
},
|
|
|
|
mP2: Boolean
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
a: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const Base = defineComponent({
|
|
|
|
props: {
|
|
|
|
p1: Boolean,
|
|
|
|
p2: {
|
|
|
|
type: Number,
|
|
|
|
default: 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
b: 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
c(): number {
|
|
|
|
return this.p2 + this.b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const MyComponent = defineComponent({
|
|
|
|
extends: Base,
|
|
|
|
mixins: [Mixin],
|
|
|
|
props: {
|
|
|
|
// required should make property non-void
|
|
|
|
z: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
const props = this.$props
|
|
|
|
// props
|
|
|
|
expectType<boolean | undefined>(props.p1)
|
|
|
|
expectType<number>(props.p2)
|
|
|
|
expectType<string>(props.z)
|
|
|
|
expectType<string>(props.mP1)
|
|
|
|
expectType<boolean | undefined>(props.mP2)
|
|
|
|
|
|
|
|
const data = this.$data
|
|
|
|
expectType<number>(data.a)
|
|
|
|
expectType<number>(data.b)
|
|
|
|
|
|
|
|
// should also expose declared props on `this`
|
|
|
|
expectType<number>(this.a)
|
|
|
|
expectType<number>(this.b)
|
|
|
|
expectType<boolean | undefined>(this.p1)
|
|
|
|
expectType<number>(this.p2)
|
|
|
|
expectType<string>(this.mP1)
|
|
|
|
expectType<boolean | undefined>(this.mP2)
|
|
|
|
|
|
|
|
// setup context properties should be mutable
|
|
|
|
this.a = 5
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Test TSX
|
|
|
|
expectType<JSX.Element>(<MyComponent mP1="p1" mP2 p1 p2={1} z={'z'} />)
|
|
|
|
|
|
|
|
// missing required props
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<MyComponent />)
|
|
|
|
|
|
|
|
// wrong prop types
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<MyComponent p2={'wrong type'} z={'z'} />)
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<MyComponent mP1={3} />)
|
|
|
|
})
|
|
|
|
|
2019-12-25 00:04:35 +08:00
|
|
|
describe('compatibility w/ createApp', () => {
|
|
|
|
const comp = defineComponent({})
|
2020-01-26 11:21:06 +08:00
|
|
|
createApp(comp).mount('#hello')
|
2019-12-25 00:04:35 +08:00
|
|
|
|
|
|
|
const comp2 = defineComponent({
|
|
|
|
props: { foo: String }
|
|
|
|
})
|
2020-01-26 11:21:06 +08:00
|
|
|
createApp(comp2).mount('#hello')
|
2019-12-25 00:04:35 +08:00
|
|
|
|
|
|
|
const comp3 = defineComponent({
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
a: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-01-26 11:21:06 +08:00
|
|
|
createApp(comp3).mount('#hello')
|
2019-12-25 00:04:35 +08:00
|
|
|
})
|
2020-01-14 06:12:45 +08:00
|
|
|
|
|
|
|
describe('defineComponent', () => {
|
2020-01-17 06:46:25 +08:00
|
|
|
test('should accept components defined with defineComponent', () => {
|
|
|
|
const comp = defineComponent({})
|
|
|
|
defineComponent({
|
|
|
|
components: { comp }
|
|
|
|
})
|
2020-01-14 06:12:45 +08:00
|
|
|
})
|
2020-08-25 09:53:30 +08:00
|
|
|
|
|
|
|
test('should accept class components with receiving constructor arguments', () => {
|
|
|
|
class Comp {
|
|
|
|
static __vccOpts = {}
|
|
|
|
constructor(_props: { foo: string }) {}
|
|
|
|
}
|
|
|
|
defineComponent({
|
|
|
|
components: { Comp }
|
|
|
|
})
|
|
|
|
})
|
2020-01-14 06:12:45 +08:00
|
|
|
})
|
2020-04-04 00:05:52 +08:00
|
|
|
|
|
|
|
describe('emits', () => {
|
|
|
|
// Note: for TSX inference, ideally we want to map emits to onXXX props,
|
|
|
|
// but that requires type-level string constant concatenation as suggested in
|
|
|
|
// https://github.com/Microsoft/TypeScript/issues/12754
|
|
|
|
|
|
|
|
// The workaround for TSX users is instead of using emits, declare onXXX props
|
|
|
|
// and call them instead. Since `v-on:click` compiles to an `onClick` prop,
|
|
|
|
// this would also support other users consuming the component in templates
|
|
|
|
// with `v-on` listeners.
|
|
|
|
|
|
|
|
// with object emits
|
|
|
|
defineComponent({
|
|
|
|
emits: {
|
|
|
|
click: (n: number) => typeof n === 'number',
|
2020-07-17 06:18:52 +08:00
|
|
|
input: (b: string) => b.length > 1
|
2020-04-04 00:05:52 +08:00
|
|
|
},
|
|
|
|
setup(props, { emit }) {
|
|
|
|
emit('click', 1)
|
|
|
|
emit('input', 'foo')
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(emit('nope'))
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(emit('click'))
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(emit('click', 'foo'))
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(emit('input'))
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(emit('input', 1))
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.$emit('click', 1)
|
|
|
|
this.$emit('input', 'foo')
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(this.$emit('nope'))
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(this.$emit('click'))
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(this.$emit('click', 'foo'))
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(this.$emit('input'))
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(this.$emit('input', 1))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// with array emits
|
|
|
|
defineComponent({
|
|
|
|
emits: ['foo', 'bar'],
|
|
|
|
setup(props, { emit }) {
|
|
|
|
emit('foo')
|
|
|
|
emit('foo', 123)
|
|
|
|
emit('bar')
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(emit('nope'))
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.$emit('foo')
|
|
|
|
this.$emit('foo', 123)
|
|
|
|
this.$emit('bar')
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(this.$emit('nope'))
|
|
|
|
}
|
|
|
|
})
|
2020-07-08 23:51:03 +08:00
|
|
|
|
|
|
|
// without emits
|
|
|
|
defineComponent({
|
|
|
|
setup(props, { emit }) {
|
|
|
|
emit('test', 1)
|
|
|
|
emit('test')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// emit should be valid when ComponentPublicInstance is used.
|
|
|
|
const instance = {} as ComponentPublicInstance
|
|
|
|
instance.$emit('test', 1)
|
|
|
|
instance.$emit('test')
|
2020-08-15 05:36:26 +08:00
|
|
|
|
|
|
|
// `this` should be void inside of emits validators
|
|
|
|
defineComponent({
|
|
|
|
props: ['bar'],
|
|
|
|
emits: {
|
|
|
|
foo(): boolean {
|
|
|
|
// @ts-expect-error
|
|
|
|
return this.bar === 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-04-04 00:05:52 +08:00
|
|
|
})
|
2020-07-15 21:19:20 +08:00
|
|
|
|
|
|
|
describe('componentOptions setup should be `SetupContext`', () => {
|
|
|
|
expect<ComponentOptions['setup']>({} as (
|
|
|
|
props: Record<string, any>,
|
|
|
|
ctx: SetupContext
|
|
|
|
) => any)
|
|
|
|
})
|
2020-08-25 09:37:22 +08:00
|
|
|
|
|
|
|
describe('extract instance type', () => {
|
|
|
|
const Base = defineComponent({
|
|
|
|
props: {
|
|
|
|
baseA: {
|
|
|
|
type: Number,
|
|
|
|
default: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const MixinA = defineComponent({
|
|
|
|
props: {
|
|
|
|
mA: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const CompA = defineComponent({
|
|
|
|
extends: Base,
|
|
|
|
mixins: [MixinA],
|
|
|
|
props: {
|
|
|
|
a: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
b: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
c: Number
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const compA = {} as InstanceType<typeof CompA>
|
|
|
|
|
|
|
|
expectType<boolean>(compA.a)
|
|
|
|
expectType<string>(compA.b)
|
|
|
|
expectType<number | undefined>(compA.c)
|
|
|
|
// mixins
|
|
|
|
expectType<string>(compA.mA)
|
|
|
|
// extends
|
|
|
|
expectType<number>(compA.baseA)
|
|
|
|
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError((compA.a = true))
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError((compA.b = 'foo'))
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError((compA.c = 1))
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError((compA.mA = 'foo'))
|
|
|
|
// @ts-expect-error
|
|
|
|
expectError((compA.baseA = 1))
|
|
|
|
})
|