2019-11-05 07:38:55 +08:00
|
|
|
import { expectError, expectType } from 'tsd'
|
2020-02-20 01:29:01 +08:00
|
|
|
import {
|
|
|
|
describe,
|
|
|
|
defineComponent,
|
|
|
|
PropType,
|
|
|
|
ref,
|
|
|
|
reactive,
|
|
|
|
createApp
|
|
|
|
} 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
|
|
|
|
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 }
|
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'
|
|
|
|
},
|
|
|
|
// 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
|
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)
|
|
|
|
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)
|
2019-11-05 07:38:55 +08:00
|
|
|
|
2019-11-09 23:03:58 +08:00
|
|
|
// props should be readonly
|
|
|
|
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)
|
2019-11-05 07:38:55 +08:00
|
|
|
|
2019-11-10 07:40:25 +08:00
|
|
|
// props should be readonly
|
|
|
|
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)
|
2019-11-05 07:38:55 +08:00
|
|
|
|
2019-11-10 07:40:25 +08:00
|
|
|
// props on `this` should be readonly
|
|
|
|
expectError((this.a = 1))
|
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
// assert setup context unwrapping
|
|
|
|
expectType<number>(this.c)
|
|
|
|
expectType<string>(this.d.e)
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// 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 })}
|
2019-11-05 07:38:55 +08:00
|
|
|
// should allow extraneous as attrs
|
|
|
|
class="bar"
|
|
|
|
// should allow key
|
|
|
|
key={'foo'}
|
|
|
|
// should allow ref
|
|
|
|
ref={'foo'}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
// missing required props
|
|
|
|
expectError(<MyComponent />)
|
|
|
|
|
|
|
|
// wrong prop types
|
|
|
|
expectError(
|
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-04-03 22:51:17 +08:00
|
|
|
expectError(<MyComponent b="foo" dd={{ n: 'string' }} ddd={['foo']} />)
|
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', () => {
|
2019-12-22 23:58:12 +08:00
|
|
|
defineComponent({
|
2019-11-05 07:38:55 +08:00
|
|
|
props: ['a', 'b'],
|
|
|
|
setup(props) {
|
2019-11-10 07:40:25 +08:00
|
|
|
// props should be readonly
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
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-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',
|
|
|
|
input: (b: string) => null
|
|
|
|
},
|
|
|
|
setup(props, { emit }) {
|
|
|
|
emit('click', 1)
|
|
|
|
emit('input', 'foo')
|
|
|
|
expectError(emit('nope'))
|
|
|
|
expectError(emit('click'))
|
|
|
|
expectError(emit('click', 'foo'))
|
|
|
|
expectError(emit('input'))
|
|
|
|
expectError(emit('input', 1))
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.$emit('click', 1)
|
|
|
|
this.$emit('input', 'foo')
|
|
|
|
expectError(this.$emit('nope'))
|
|
|
|
expectError(this.$emit('click'))
|
|
|
|
expectError(this.$emit('click', 'foo'))
|
|
|
|
expectError(this.$emit('input'))
|
|
|
|
expectError(this.$emit('input', 1))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// with array emits
|
|
|
|
defineComponent({
|
|
|
|
emits: ['foo', 'bar'],
|
|
|
|
setup(props, { emit }) {
|
|
|
|
emit('foo')
|
|
|
|
emit('foo', 123)
|
|
|
|
emit('bar')
|
|
|
|
expectError(emit('nope'))
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.$emit('foo')
|
|
|
|
this.$emit('foo', 123)
|
|
|
|
this.$emit('bar')
|
|
|
|
expectError(this.$emit('nope'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|