import { expectError, expectType } from 'tsd' import { describe, defineComponent, PropType, ref, createApp } from './index' describe('with object props', () => { interface ExpectedProps { a?: number | undefined b: string bb: string cc?: string[] | undefined dd: string[] ccc?: string[] | undefined ddd: string[] } type GT = string & { __brand: unknown } const MyComponent = defineComponent({ props: { a: Number, // required should make property non-void b: { type: String, required: true }, // default value should infer type and make it non-void bb: { default: 'hello' }, // explicit type casting cc: Array as PropType, // required + type casting dd: { type: Array as PropType, required: true }, // explicit type casting with constructor ccc: Array as () => string[], // required + contructor type casting ddd: { type: Array as () => string[], required: true } }, setup(props) { // type assertion. See https://github.com/SamVerschueren/tsd expectType(props.a) expectType(props.b) expectType(props.bb) expectType(props.cc) expectType(props.dd) expectType(props.ccc) expectType(props.ddd) // props should be readonly expectError((props.a = 1)) // setup context return { c: ref(1), d: { e: ref('hi') }, f: { g: ref('hello' as GT) } } }, render() { const props = this.$props expectType(props.a) expectType(props.b) expectType(props.bb) expectType(props.cc) expectType(props.dd) expectType(props.ccc) expectType(props.ddd) // props should be readonly expectError((props.a = 1)) // should also expose declared props on `this` expectType(this.a) expectType(this.b) expectType(this.bb) expectType(this.cc) expectType(this.dd) expectType(this.ccc) expectType(this.ddd) // props on `this` should be readonly expectError((this.a = 1)) // assert setup context unwrapping expectType(this.c) expectType(this.d.e) expectType(this.f.g) // setup context properties should be mutable this.c = 2 return null } }) // Test TSX expectType( ) // missing required props expectError() // wrong prop types expectError( ) expectError() }) describe('type inference w/ optional props declaration', () => { const MyComponent = defineComponent({ setup(_props: { msg: string }) { return { a: 1 } }, render() { expectType(this.$props.msg) // props should be readonly expectError((this.$props.msg = 'foo')) // should not expose on `this` expectError(this.msg) expectType(this.a) return null } }) expectType() expectError() expectError() }) describe('type inference w/ direct setup function', () => { const MyComponent = defineComponent((_props: { msg: string }) => {}) expectType() expectError() expectError() }) describe('type inference w/ array props declaration', () => { defineComponent({ props: ['a', 'b'], setup(props) { // props should be readonly expectError((props.a = 1)) expectType(props.a) expectType(props.b) return { c: 1 } }, render() { expectType(this.$props.a) expectType(this.$props.b) expectError((this.$props.a = 1)) expectType(this.a) expectType(this.b) expectType(this.c) } }) }) describe('type inference w/ options API', () => { defineComponent({ 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(this.a) return { c: this.a || 123 } }, computed: { d(): number { expectType(this.b) return this.b + 1 } }, watch: { a() { expectType(this.b) this.b + 1 } }, created() { // props expectType(this.a) // returned from setup() expectType(this.b) // returned from data() expectType(this.c) // computed expectType(this.d) }, methods: { doSomething() { // props expectType(this.a) // returned from setup() expectType(this.b) // returned from data() expectType(this.c) // computed expectType(this.d) } }, render() { // props expectType(this.a) // returned from setup() expectType(this.b) // returned from data() expectType(this.c) // computed expectType(this.d) } }) }) describe('compatibility w/ createApp', () => { const comp = defineComponent({}) createApp(comp).mount('#hello') const comp2 = defineComponent({ props: { foo: String } }) createApp(comp2).mount('#hello') const comp3 = defineComponent({ setup() { return { a: 1 } } }) createApp(comp3).mount('#hello') }) describe('defineComponent', () => { test('should accept components defined with defineComponent', () => { const comp = defineComponent({}) defineComponent({ components: { comp } }) }) })