| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221 |
- import {
- type Component,
- type ComponentOptions,
- type ComponentPublicInstance,
- type PropType,
- type SetupContext,
- type Slots,
- type SlotsType,
- type VNode,
- createApp,
- defineComponent,
- h,
- reactive,
- ref,
- withKeys,
- withModifiers,
- } from 'vue'
- import { type IsAny, type IsUnion, describe, expectType } from './utils'
- describe('with object props', () => {
- interface ExpectedProps {
- a?: number | undefined
- aa: number
- aaa: number | null
- aaaa: number | undefined
- b: string
- e?: Function
- h: boolean
- j: undefined | (() => string | undefined)
- bb: string
- bbb: string
- bbbb: string | undefined
- bbbbb: string | undefined
- cc?: string[] | undefined
- dd: { n: 1 }
- ee?: () => string
- ff?: (a: number, b: string) => { a: boolean }
- ccc?: string[] | undefined
- ddd: string[]
- eee: () => { a: string }
- fff: (a: number, b: string) => { a: boolean }
- hhh: boolean
- ggg: 'foo' | 'bar'
- ffff: (a: number, b: string) => { a: boolean }
- iii?: (() => string) | (() => number)
- jjj: ((arg1: string) => string) | ((arg1: string, arg2: string) => string)
- kkk?: any
- validated?: string
- date?: Date
- l?: Date
- ll?: Date | number
- lll?: string | number
- }
- type GT = string & { __brand: unknown }
- const props = {
- a: Number,
- aa: {
- type: Number as PropType<number | undefined>,
- default: 1,
- },
- aaa: {
- type: Number as PropType<number | null>,
- default: 1,
- },
- aaaa: {
- type: Number as PropType<number | undefined>,
- // `as const` prevents widening to `boolean` (keeps literal `true` type)
- required: true as const,
- },
- // required should make property non-void
- b: {
- type: String,
- required: true as true,
- },
- e: Function,
- h: Boolean,
- j: Function as PropType<undefined | (() => string | undefined)>,
- // default value should infer type and make it non-void
- bb: {
- default: 'hello',
- },
- bbb: {
- // Note: default function value requires arrow syntax + explicit
- // annotation
- default: (props: any) => (props.bb as string) || 'foo',
- },
- bbbb: {
- type: String,
- default: undefined,
- },
- bbbbb: {
- type: String,
- default: () => undefined,
- },
- // explicit type casting
- cc: Array as PropType<string[]>,
- // required + type casting
- dd: {
- type: Object as PropType<{ n: 1 }>,
- required: true as true,
- },
- // return type
- ee: Function as PropType<() => string>,
- // arguments + object return
- ff: Function as PropType<(a: number, b: string) => { a: boolean }>,
- // explicit type casting with constructor
- ccc: Array as () => string[],
- // required + constructor type casting
- ddd: {
- type: Array as () => string[],
- required: true as true,
- },
- // required + object return
- eee: {
- type: Function as PropType<() => { a: string }>,
- required: true as true,
- },
- // required + arguments + object return
- fff: {
- type: Function as PropType<(a: number, b: string) => { a: boolean }>,
- required: true as true,
- },
- hhh: {
- type: Boolean,
- required: true as true,
- },
- // default + type casting
- ggg: {
- type: String as PropType<'foo' | 'bar'>,
- default: 'foo',
- },
- // default + function
- ffff: {
- type: Function as PropType<(a: number, b: string) => { a: boolean }>,
- default: (a: number, b: string) => ({ a: a > +b }),
- },
- // union + function with different return types
- iii: Function as PropType<(() => string) | (() => number)>,
- // union + function with different args & same return type
- jjj: {
- type: Function as PropType<
- ((arg1: string) => string) | ((arg1: string, arg2: string) => string)
- >,
- required: true as true,
- },
- kkk: null,
- validated: {
- type: String,
- // validator requires explicit annotation
- validator: (val: unknown) => val !== '',
- },
- date: Date,
- l: [Date],
- ll: [Date, Number],
- lll: [String, Number],
- }
- const MyComponent = defineComponent({
- props,
- setup(props) {
- // type assertion. See https://github.com/SamVerschueren/tsd
- expectType<ExpectedProps['a']>(props.a)
- expectType<ExpectedProps['aa']>(props.aa)
- expectType<ExpectedProps['aaa']>(props.aaa)
- // @ts-expect-error should included `undefined`
- expectType<number>(props.aaaa)
- expectType<ExpectedProps['aaaa']>(props.aaaa)
- expectType<ExpectedProps['b']>(props.b)
- expectType<ExpectedProps['e']>(props.e)
- expectType<ExpectedProps['h']>(props.h)
- expectType<ExpectedProps['j']>(props.j)
- expectType<ExpectedProps['bb']>(props.bb)
- expectType<ExpectedProps['bbb']>(props.bbb)
- expectType<ExpectedProps['bbbb']>(props.bbbb)
- expectType<ExpectedProps['bbbbb']>(props.bbbbb)
- expectType<ExpectedProps['cc']>(props.cc)
- expectType<ExpectedProps['dd']>(props.dd)
- expectType<ExpectedProps['ee']>(props.ee)
- expectType<ExpectedProps['ff']>(props.ff)
- expectType<ExpectedProps['ccc']>(props.ccc)
- expectType<ExpectedProps['ddd']>(props.ddd)
- expectType<ExpectedProps['eee']>(props.eee)
- expectType<ExpectedProps['fff']>(props.fff)
- expectType<ExpectedProps['hhh']>(props.hhh)
- expectType<ExpectedProps['ggg']>(props.ggg)
- expectType<ExpectedProps['ffff']>(props.ffff)
- if (typeof props.iii !== 'function') {
- expectType<undefined>(props.iii)
- }
- expectType<ExpectedProps['iii']>(props.iii)
- expectType<IsUnion<typeof props.jjj>>(true)
- expectType<ExpectedProps['jjj']>(props.jjj)
- expectType<ExpectedProps['kkk']>(props.kkk)
- expectType<ExpectedProps['validated']>(props.validated)
- expectType<ExpectedProps['date']>(props.date)
- expectType<ExpectedProps['l']>(props.l)
- expectType<ExpectedProps['ll']>(props.ll)
- expectType<ExpectedProps['lll']>(props.lll)
- // @ts-expect-error props should be readonly
- props.a = 1
- // setup context
- return {
- c: ref(1),
- d: {
- e: ref('hi'),
- },
- f: reactive({
- g: ref('hello' as GT),
- }),
- }
- },
- provide() {
- return {}
- },
- render() {
- const props = this.$props
- expectType<ExpectedProps['a']>(props.a)
- expectType<ExpectedProps['aa']>(props.aa)
- expectType<ExpectedProps['aaa']>(props.aaa)
- expectType<ExpectedProps['b']>(props.b)
- expectType<ExpectedProps['e']>(props.e)
- expectType<ExpectedProps['h']>(props.h)
- expectType<ExpectedProps['bb']>(props.bb)
- expectType<ExpectedProps['cc']>(props.cc)
- expectType<ExpectedProps['dd']>(props.dd)
- expectType<ExpectedProps['ee']>(props.ee)
- expectType<ExpectedProps['ff']>(props.ff)
- expectType<ExpectedProps['ccc']>(props.ccc)
- expectType<ExpectedProps['ddd']>(props.ddd)
- expectType<ExpectedProps['eee']>(props.eee)
- expectType<ExpectedProps['fff']>(props.fff)
- expectType<ExpectedProps['hhh']>(props.hhh)
- expectType<ExpectedProps['ggg']>(props.ggg)
- if (typeof props.iii !== 'function') {
- expectType<undefined>(props.iii)
- }
- expectType<ExpectedProps['iii']>(props.iii)
- expectType<IsUnion<typeof props.jjj>>(true)
- expectType<ExpectedProps['jjj']>(props.jjj)
- expectType<ExpectedProps['kkk']>(props.kkk)
- // @ts-expect-error props should be readonly
- props.a = 1
- // should also expose declared props on `this`
- expectType<ExpectedProps['a']>(this.a)
- expectType<ExpectedProps['aa']>(this.aa)
- expectType<ExpectedProps['aaa']>(this.aaa)
- expectType<ExpectedProps['b']>(this.b)
- expectType<ExpectedProps['e']>(this.e)
- expectType<ExpectedProps['h']>(this.h)
- expectType<ExpectedProps['bb']>(this.bb)
- expectType<ExpectedProps['cc']>(this.cc)
- expectType<ExpectedProps['dd']>(this.dd)
- expectType<ExpectedProps['ee']>(this.ee)
- expectType<ExpectedProps['ff']>(this.ff)
- expectType<ExpectedProps['ccc']>(this.ccc)
- expectType<ExpectedProps['ddd']>(this.ddd)
- expectType<ExpectedProps['eee']>(this.eee)
- expectType<ExpectedProps['fff']>(this.fff)
- expectType<ExpectedProps['hhh']>(this.hhh)
- expectType<ExpectedProps['ggg']>(this.ggg)
- if (typeof this.iii !== 'function') {
- expectType<undefined>(this.iii)
- }
- expectType<ExpectedProps['iii']>(this.iii)
- const { jjj } = this
- expectType<IsUnion<typeof jjj>>(true)
- expectType<ExpectedProps['jjj']>(this.jjj)
- expectType<ExpectedProps['kkk']>(this.kkk)
- // @ts-expect-error props on `this` should be readonly
- this.a = 1
- // assert setup context unwrapping
- expectType<number>(this.c)
- expectType<string>(this.d.e.value)
- expectType<GT>(this.f.g)
- // setup context properties should be mutable
- this.c = 2
- return null
- },
- })
- expectType<Component>(MyComponent)
- // Test TSX
- expectType<JSX.Element>(
- <MyComponent
- a={1}
- aaaa={1}
- b="b"
- bb="bb"
- e={() => {}}
- cc={['cc']}
- dd={{ n: 1 }}
- ee={() => 'ee'}
- ccc={['ccc']}
- ddd={['ddd']}
- eee={() => ({ a: 'eee' })}
- fff={(a, b) => ({ a: a > +b })}
- hhh={false}
- ggg="foo"
- jjj={() => ''}
- // should allow class/style as attrs
- class="bar"
- style={{ color: 'red' }}
- // should allow key
- key={'foo'}
- // should allow ref
- ref={'foo'}
- ref_for={true}
- />,
- )
- expectType<Component>(
- <MyComponent
- aaaa={1}
- b="b"
- dd={{ n: 1 }}
- ddd={['ddd']}
- eee={() => ({ a: 'eee' })}
- fff={(a, b) => ({ a: a > +b })}
- hhh={false}
- jjj={() => ''}
- />,
- )
- // @ts-expect-error missing required props
- let c = <MyComponent />
- // @ts-expect-error wrong prop types
- c = <MyComponent a={'wrong type'} b="foo" dd={{ n: 1 }} ddd={['foo']} />
- // @ts-expect-error wrong prop types
- c = <MyComponent ggg="baz" />
- // @ts-expect-error
- ;<MyComponent b="foo" dd={{ n: 'string' }} ddd={['foo']} />
- // `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,
- },
- },
- })
- })
- describe('type inference w/ optional props declaration', () => {
- const MyComponent = defineComponent<{ a: string[]; msg: string }>({
- setup(props) {
- expectType<string>(props.msg)
- expectType<string[]>(props.a)
- return {
- b: 1,
- }
- },
- })
- expectType<JSX.Element>(<MyComponent msg="1" a={['1']} />)
- // @ts-expect-error
- ;<MyComponent />
- // @ts-expect-error
- ;<MyComponent msg="1" />
- })
- describe('type inference w/ direct setup function', () => {
- const MyComponent = defineComponent((_props: { msg: string }) => () => {})
- expectType<JSX.Element>(<MyComponent msg="foo" />)
- // @ts-expect-error
- ;<MyComponent />
- // @ts-expect-error
- ;<MyComponent msg={1} />
- })
- describe('type inference w/ array props declaration', () => {
- const MyComponent = defineComponent({
- props: ['a', 'b'],
- setup(props) {
- // @ts-expect-error props should be readonly
- props.a = 1
- expectType<any>(props.a)
- expectType<any>(props.b)
- return {
- c: 1,
- }
- },
- render() {
- expectType<any>(this.$props.a)
- expectType<any>(this.$props.b)
- // @ts-expect-error
- this.$props.a = 1
- expectType<any>(this.a)
- expectType<any>(this.b)
- expectType<number>(this.c)
- },
- })
- expectType<JSX.Element>(<MyComponent a={[1, 2]} b="b" />)
- // @ts-expect-error
- ;<MyComponent other="other" />
- })
- 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<number | undefined>(this.a)
- return {
- c: this.a || 123,
- someRef: ref(0),
- }
- },
- computed: {
- d() {
- expectType<number>(this.b)
- return this.b + 1
- },
- e: {
- get() {
- 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)
- },
- },
- },
- 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)
- // computed get/set
- expectType<number>(this.e)
- expectType<number>(this.someRef)
- },
- 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)
- // computed get/set
- expectType<number>(this.e)
- },
- returnSomething() {
- return this.a
- },
- },
- 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)
- // computed get/set
- expectType<number>(this.e)
- // method
- expectType<() => number | undefined>(this.returnSomething)
- },
- })
- })
- // #4051
- describe('type inference w/ empty prop object', () => {
- const MyComponent = defineComponent({
- props: {},
- setup(props) {
- return {}
- },
- render() {},
- })
- expectType<JSX.Element>(<MyComponent />)
- // AllowedComponentProps
- expectType<JSX.Element>(<MyComponent class={'foo'} />)
- // ComponentCustomProps
- expectType<JSX.Element>(<MyComponent custom={1} />)
- // VNodeProps
- expectType<JSX.Element>(<MyComponent key="1" />)
- // @ts-expect-error
- expectError(<MyComponent other="other" />)
- })
- describe('with mixins', () => {
- const MixinA = defineComponent({
- emits: ['bar'],
- 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() {
- //@ts-expect-error computed are not available on data()
- expectError<number>(this.dC1)
- //@ts-expect-error computed are not available on data()
- expectError<string>(this.dC2)
- return {
- d: 4,
- }
- },
- setup(props) {
- expectType<string>(props.aP1)
- },
- computed: {
- dC1() {
- return this.d + this.a
- },
- dC2() {
- return this.aP1 + 'dC2'
- },
- },
- })
- const MyComponent = defineComponent({
- mixins: [MixinA, MixinB, MixinC, MixinD],
- emits: ['click'],
- props: {
- // required should make property non-void
- z: {
- type: String,
- required: true,
- },
- },
- data(vm) {
- expectType<number>(vm.a)
- expectType<number>(vm.b)
- expectType<number>(vm.c)
- expectType<number>(vm.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)
- return {}
- },
- setup(props) {
- expectType<string>(props.z)
- // props
- expectType<((...args: any[]) => any) | undefined>(props.onClick)
- // from MixinA
- expectType<((...args: any[]) => any) | undefined>(props.onBar)
- expectType<string>(props.aP1)
- expectType<boolean | undefined>(props.aP2)
- expectType<any>(props.bP1)
- expectType<any>(props.bP2)
- expectType<string>(props.z)
- },
- render() {
- const props = this.$props
- // props
- expectType<((...args: any[]) => any) | undefined>(props.onClick)
- // from MixinA
- expectType<((...args: any[]) => any) | undefined>(props.onBar)
- 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
- this.aP1 = 'new'
- // @ts-expect-error
- this.z = 1
- // props on `this` should be readonly
- // @ts-expect-error
- this.bP1 = 1
- // string value can not assigned to number type value
- // @ts-expect-error
- 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
- ;<MyComponent />
- // wrong prop types
- // @ts-expect-error
- ;<MyComponent aP1="ap" aP2={'wrong type'} bP1="b" z={'z'} />
- // @ts-expect-error
- ;<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
- ;<MyComponent />
- // wrong prop types
- // @ts-expect-error
- ;<MyComponent aP2={'wrong type'} z={'z'} />
- // @ts-expect-error
- ;<MyComponent aP1={3} />
- })
- describe('extends with mixins', () => {
- const Mixin = defineComponent({
- emits: ['bar'],
- props: {
- mP1: {
- type: String,
- default: 'mP1',
- },
- mP2: Boolean,
- mP3: {
- type: Boolean,
- required: true,
- },
- },
- data() {
- return {
- a: 1,
- }
- },
- })
- const Base = defineComponent({
- emits: ['foo'],
- props: {
- p1: Boolean,
- p2: {
- type: Number,
- default: 2,
- },
- p3: {
- type: Boolean,
- required: true,
- },
- },
- data() {
- return {
- b: 2,
- }
- },
- computed: {
- c(): number {
- return this.p2 + this.b
- },
- },
- })
- const MyComponent = defineComponent({
- extends: Base,
- mixins: [Mixin],
- emits: ['click'],
- props: {
- // required should make property non-void
- z: {
- type: String,
- required: true,
- },
- },
- render() {
- const props = this.$props
- // props
- expectType<((...args: any[]) => any) | undefined>(props.onClick)
- // from Mixin
- expectType<((...args: any[]) => any) | undefined>(props.onBar)
- // from Base
- expectType<((...args: any[]) => any) | undefined>(props.onFoo)
- 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 mP3 p1 p2={1} p3 z={'z'} />)
- // mP1, mP2, p1, and p2 have default value. these are not required
- expectType<JSX.Element>(<MyComponent mP3 p3 z={'z'} />)
- // missing required props
- // @ts-expect-error
- ;<MyComponent mP3 p3 /* z='z' */ />
- // missing required props from mixin
- // @ts-expect-error
- ;<MyComponent /* mP3 */ p3 z="z" />
- // missing required props from extends
- // @ts-expect-error
- ;<MyComponent mP3 /* p3 */ z="z" />
- // wrong prop types
- // @ts-expect-error
- ;<MyComponent p2={'wrong type'} z={'z'} />
- // @ts-expect-error
- ;<MyComponent mP1={3} />
- // #3468
- const CompWithD = defineComponent({
- data() {
- return { foo: 1 }
- },
- })
- const CompWithC = defineComponent({
- computed: {
- foo() {
- return 1
- },
- },
- })
- const CompWithM = defineComponent({ methods: { foo() {} } })
- const CompEmpty = defineComponent({})
- defineComponent({
- mixins: [CompWithD, CompEmpty],
- mounted() {
- expectType<number>(this.foo)
- },
- })
- defineComponent({
- mixins: [CompWithC, CompEmpty],
- mounted() {
- expectType<number>(this.foo)
- },
- })
- defineComponent({
- mixins: [CompWithM, CompEmpty],
- mounted() {
- expectType<() => void>(this.foo)
- },
- })
- })
- 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', () => {
- describe('should accept components defined with defineComponent', () => {
- const comp = defineComponent({})
- defineComponent({
- components: { comp },
- })
- })
- describe('should accept class components with receiving constructor arguments', () => {
- class Comp {
- static __vccOpts = {}
- constructor(_props: { foo: string }) {}
- }
- defineComponent({
- components: { Comp },
- })
- })
- })
- 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) => b.length > 1,
- Focus: (f: boolean) => !!f,
- },
- setup(props, { emit }) {
- expectType<((n: number) => boolean) | undefined>(props.onClick)
- expectType<((b: string) => boolean) | undefined>(props.onInput)
- expectType<((f: boolean) => boolean) | undefined>(props.onFocus)
- emit('click', 1)
- emit('input', 'foo')
- emit('Focus', true)
- // @ts-expect-error
- emit('nope')
- // @ts-expect-error
- emit('click')
- // @ts-expect-error
- emit('click', 'foo')
- // @ts-expect-error
- emit('input')
- // @ts-expect-error
- emit('input', 1)
- // @ts-expect-error
- emit('focus')
- // @ts-expect-error
- emit('focus', true)
- },
- created() {
- this.$emit('click', 1)
- this.$emit('input', 'foo')
- // @ts-expect-error
- this.$emit('nope')
- // @ts-expect-error
- this.$emit('click')
- // @ts-expect-error
- this.$emit('click', 'foo')
- // @ts-expect-error
- this.$emit('input')
- // @ts-expect-error
- this.$emit('input', 1)
- // @ts-expect-error
- this.$emit('focus')
- // @ts-expect-error
- this.$emit('focus', true)
- },
- mounted() {
- // #3599
- this.$nextTick(function () {
- // this should be bound to this instance
- this.$emit('click', 1)
- this.$emit('input', 'foo')
- // @ts-expect-error
- this.$emit('nope')
- // @ts-expect-error
- this.$emit('click')
- // @ts-expect-error
- this.$emit('click', 'foo')
- // @ts-expect-error
- this.$emit('input')
- // @ts-expect-error
- this.$emit('input', 1)
- // @ts-expect-error
- this.$emit('focus')
- // @ts-expect-error
- this.$emit('focus', true)
- })
- },
- })
- // with array emits
- defineComponent({
- emits: ['foo', 'bar'],
- setup(props, { emit }) {
- expectType<((...args: any[]) => any) | undefined>(props.onFoo)
- expectType<((...args: any[]) => any) | undefined>(props.onBar)
- emit('foo')
- emit('foo', 123)
- emit('bar')
- // @ts-expect-error
- emit('nope')
- },
- created() {
- this.$emit('foo')
- this.$emit('foo', 123)
- this.$emit('bar')
- // @ts-expect-error
- this.$emit('nope')
- },
- })
- // with tsx
- const Component = defineComponent({
- emits: {
- click: (n: number) => typeof n === 'number',
- },
- setup(props, { emit }) {
- expectType<((n: number) => any) | undefined>(props.onClick)
- emit('click', 1)
- // @ts-expect-error
- emit('click')
- // @ts-expect-error
- emit('click', 'foo')
- },
- })
- defineComponent({
- render() {
- return (
- <Component
- onClick={(n: number) => {
- return n + 1
- }}
- />
- )
- },
- })
- // #11803 manual props annotation in setup()
- const Hello = defineComponent({
- name: 'HelloWorld',
- inheritAttrs: false,
- props: { foo: String },
- emits: {
- customClick: (args: string) => typeof args === 'string',
- },
- setup(props: { foo?: string }) {},
- })
- ;<Hello onCustomClick={() => {}} />
- // 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')
- // `this` should be void inside of emits validators
- defineComponent({
- props: ['bar'],
- emits: {
- foo(): boolean {
- // @ts-expect-error
- return this.bar === 3
- },
- },
- })
- })
- describe('inject', () => {
- // with object inject
- defineComponent({
- props: {
- a: String,
- },
- inject: {
- foo: 'foo',
- bar: 'bar',
- },
- created() {
- expectType<unknown>(this.foo)
- expectType<unknown>(this.bar)
- // @ts-expect-error
- this.foobar = 1
- },
- })
- // with array inject
- defineComponent({
- props: ['a', 'b'],
- inject: ['foo', 'bar'],
- created() {
- expectType<unknown>(this.foo)
- expectType<unknown>(this.bar)
- // @ts-expect-error
- this.foobar = 1
- },
- })
- // with no props
- defineComponent({
- inject: {
- foo: {
- from: 'pfoo',
- default: 'foo',
- },
- bar: {
- from: 'pbar',
- default: 'bar',
- },
- },
- created() {
- expectType<unknown>(this.foo)
- expectType<unknown>(this.bar)
- // @ts-expect-error
- this.foobar = 1
- },
- })
- // without inject
- defineComponent({
- props: ['a', 'b'],
- created() {
- // @ts-expect-error
- this.foo = 1
- // @ts-expect-error
- this.bar = 1
- },
- })
- })
- describe('componentOptions setup should be `SetupContext`', () => {
- expectType<ComponentOptions['setup']>(
- {} as (props: Record<string, any>, ctx: SetupContext) => any,
- )
- })
- 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
- compA.a = true
- // @ts-expect-error
- compA.b = 'foo'
- // @ts-expect-error
- compA.c = 1
- // @ts-expect-error
- compA.mA = 'foo'
- // @ts-expect-error
- compA.baseA = 1
- })
- describe('async setup', () => {
- type GT = string & { __brand: unknown }
- const Comp = defineComponent({
- async setup() {
- // setup context
- return {
- a: ref(1),
- b: {
- c: ref('hi'),
- },
- d: reactive({
- e: ref('hello' as GT),
- }),
- }
- },
- render() {
- // assert setup context unwrapping
- expectType<number>(this.a)
- expectType<string>(this.b.c.value)
- expectType<GT>(this.d.e)
- // setup context properties should be mutable
- this.a = 2
- },
- })
- const vm = {} as InstanceType<typeof Comp>
- // assert setup context unwrapping
- expectType<number>(vm.a)
- expectType<string>(vm.b.c.value)
- expectType<GT>(vm.d.e)
- // setup context properties should be mutable
- vm.a = 2
- })
- // #5948
- describe('DefineComponent should infer correct types when assigning to Component', () => {
- let component: Component
- component = defineComponent({
- setup(_, { attrs, slots }) {
- // @ts-expect-error should not be any
- expectType<[]>(attrs)
- // @ts-expect-error should not be any
- expectType<[]>(slots)
- },
- })
- expectType<Component>(component)
- })
- // #5969
- describe('should allow to assign props', () => {
- const Child = defineComponent({
- props: {
- bar: String,
- },
- })
- const Parent = defineComponent({
- props: {
- ...Child.props,
- foo: String,
- },
- })
- const child = new Child()
- expectType<JSX.Element>(<Parent {...child.$props} />)
- })
- // #6052
- describe('prop starting with `on*` is broken', () => {
- defineComponent({
- props: {
- onX: {
- type: Function as PropType<(a: 1) => void>,
- required: true,
- },
- },
- setup(props) {
- expectType<(a: 1) => void>(props.onX)
- props.onX(1)
- },
- })
- defineComponent({
- props: {
- onX: {
- type: Function as PropType<(a: 1) => void>,
- required: true,
- },
- },
- emits: {
- test: (a: 1) => true,
- },
- setup(props) {
- expectType<(a: 1) => void>(props.onX)
- expectType<undefined | ((a: 1) => any)>(props.onTest)
- },
- })
- })
- describe('function syntax w/ generics', () => {
- const Comp = defineComponent(
- // TODO: babel plugin to auto infer runtime props options from type
- // similar to defineProps<{...}>()
- <T extends string | number>(props: { msg: T; list: T[] }) => {
- // use Composition API here like in <script setup>
- const count = ref(0)
- return () => (
- // return a render function (both JSX and h() works)
- <div>
- {props.msg} {count.value}
- </div>
- )
- },
- )
- expectType<JSX.Element>(<Comp msg="fse" list={['foo']} />)
- expectType<JSX.Element>(<Comp msg={123} list={[123]} />)
- expectType<JSX.Element>(
- // @ts-expect-error missing prop
- <Comp msg={123} />,
- )
- expectType<JSX.Element>(
- // @ts-expect-error generics don't match
- <Comp msg="fse" list={[123]} />,
- )
- expectType<JSX.Element>(
- // @ts-expect-error generics don't match
- <Comp msg={123} list={['123']} />,
- )
- })
- describe('function syntax w/ emits', () => {
- const Foo = defineComponent(
- (props: { msg: string }, ctx) => {
- ctx.emit('foo')
- // @ts-expect-error
- ctx.emit('bar')
- return () => {}
- },
- {
- emits: ['foo'],
- },
- )
- expectType<JSX.Element>(<Foo msg="hi" onFoo={() => {}} />)
- // @ts-expect-error
- expectType<JSX.Element>(<Foo msg="hi" onBar={() => {}} />)
- defineComponent(
- (props: { msg: string }, ctx) => {
- ctx.emit('foo', 'hi')
- // @ts-expect-error
- ctx.emit('foo')
- // @ts-expect-error
- ctx.emit('bar')
- return () => {}
- },
- {
- emits: {
- foo: (a: string) => true,
- },
- },
- )
- })
- describe('function syntax w/ runtime props', () => {
- // with runtime props, the runtime props must match
- // manual type declaration
- const Comp1 = defineComponent(
- (_props: { msg: string }) => {
- return () => {}
- },
- {
- props: ['msg'],
- },
- )
- // @ts-expect-error bar isn't specified in props definition
- defineComponent(
- (_props: { msg: string }) => {
- return () => {}
- },
- {
- props: ['msg', 'bar'],
- },
- )
- defineComponent(
- (_props: { msg: string; bar: string }) => {
- return () => {}
- },
- {
- props: ['msg'],
- },
- )
- expectType<JSX.Element>(<Comp1 msg="1" />)
- // @ts-expect-error msg type is incorrect
- expectType<JSX.Element>(<Comp1 msg={1} />)
- // @ts-expect-error msg is missing
- expectType<JSX.Element>(<Comp1 />)
- // @ts-expect-error bar doesn't exist
- expectType<JSX.Element>(<Comp1 msg="1" bar="2" />)
- const Comp2 = defineComponent(
- <T extends string>(_props: { msg: T }) => {
- return () => {}
- },
- {
- props: ['msg'],
- },
- )
- // @ts-expect-error bar isn't specified in props definition
- defineComponent(
- <T extends string>(_props: { msg: T }) => {
- return () => {}
- },
- {
- props: ['msg', 'bar'],
- },
- )
- defineComponent(
- <T extends string>(_props: { msg: T; bar: T }) => {
- return () => {}
- },
- {
- props: ['msg'],
- },
- )
- expectType<JSX.Element>(<Comp2 msg="1" />)
- expectType<JSX.Element>(<Comp2<string> msg="1" />)
- // @ts-expect-error msg type is incorrect
- expectType<JSX.Element>(<Comp2 msg={1} />)
- // @ts-expect-error msg is missing
- expectType<JSX.Element>(<Comp2 />)
- // @ts-expect-error bar doesn't exist
- expectType<JSX.Element>(<Comp2 msg="1" bar="2" />)
- // Note: generics aren't supported with object runtime props
- const Comp3 = defineComponent(
- <T extends string>(_props: { msg: T }) => {
- return () => {}
- },
- {
- props: {
- msg: String,
- },
- },
- )
- defineComponent(
- // @ts-expect-error bar isn't specified in props definition
- <T extends string>(_props: { msg: T }) => {
- return () => {}
- },
- {
- props: {
- bar: String,
- },
- },
- )
- defineComponent(
- // @ts-expect-error generics aren't supported with object runtime props
- <T extends string>(_props: { msg: T; bar: T }) => {
- return () => {}
- },
- {
- props: {
- msg: String,
- },
- },
- )
- expectType<JSX.Element>(<Comp3 msg="1" />)
- // @ts-expect-error generics aren't supported with object runtime props
- expectType<JSX.Element>(<Comp3<string> msg="1" />)
- // @ts-expect-error msg type is incorrect
- expectType<JSX.Element>(<Comp3 msg={1} />)
- // @ts-expect-error msg is missing
- expectType<JSX.Element>(<Comp3 />)
- // @ts-expect-error bar doesn't exist
- expectType<JSX.Element>(<Comp3 msg="1" bar="2" />)
- // @ts-expect-error string prop names don't match
- defineComponent(
- (_props: { msg: string }) => {
- return () => {}
- },
- {
- props: ['bar'],
- },
- )
- defineComponent(
- (_props: { msg: string }) => {
- return () => {}
- },
- {
- props: {
- // @ts-expect-error prop type mismatch
- msg: Number,
- },
- },
- )
- })
- // check if defineComponent can be exported
- export default {
- // function components
- a: defineComponent(_ => () => h('div')),
- // no props
- b: defineComponent({
- data() {
- return {}
- },
- }),
- c: defineComponent({
- props: ['a'],
- }),
- d: defineComponent({
- props: {
- a: Number,
- },
- }),
- }
- describe('slots', () => {
- const comp1 = defineComponent({
- slots: Object as SlotsType<{
- default: { foo: string; bar: number }
- optional?: { data: string }
- undefinedScope: undefined | { data: string }
- optionalUndefinedScope?: undefined | { data: string }
- }>,
- setup(props, { slots }) {
- expectType<(scope: { foo: string; bar: number }) => VNode[]>(
- slots.default,
- )
- expectType<((scope: { data: string }) => VNode[]) | undefined>(
- slots.optional,
- )
- slots.default({ foo: 'foo', bar: 1 })
- // @ts-expect-error it's optional
- slots.optional({ data: 'foo' })
- slots.optional?.({ data: 'foo' })
- expectType<{
- (): VNode[]
- (scope: undefined | { data: string }): VNode[]
- }>(slots.undefinedScope)
- expectType<
- | { (): VNode[]; (scope: undefined | { data: string }): VNode[] }
- | undefined
- >(slots.optionalUndefinedScope)
- slots.default({ foo: 'foo', bar: 1 })
- // @ts-expect-error it's optional
- slots.optional({ data: 'foo' })
- slots.optional?.({ data: 'foo' })
- slots.undefinedScope()
- slots.undefinedScope(undefined)
- // @ts-expect-error
- slots.undefinedScope('foo')
- slots.optionalUndefinedScope?.()
- slots.optionalUndefinedScope?.(undefined)
- slots.optionalUndefinedScope?.({ data: 'foo' })
- // @ts-expect-error
- slots.optionalUndefinedScope()
- // @ts-expect-error
- slots.optionalUndefinedScope?.('foo')
- expectType<typeof slots | undefined>(new comp1().$slots)
- },
- })
- const comp2 = defineComponent({
- setup(props, { slots }) {
- // unknown slots
- expectType<Slots>(slots)
- expectType<((...args: any[]) => VNode[]) | undefined>(slots.default)
- },
- })
- expectType<Slots | undefined>(new comp2().$slots)
- })
- // #5885
- describe('should work when props type is incompatible with setup returned type ', () => {
- type SizeType = 'small' | 'big'
- const Comp = defineComponent({
- props: {
- size: {
- type: String as PropType<SizeType>,
- required: true,
- },
- },
- setup(props) {
- expectType<SizeType>(props.size)
- return {
- size: 1,
- }
- },
- })
- type CompInstance = InstanceType<typeof Comp>
- const CompA = {} as CompInstance
- expectType<ComponentPublicInstance>(CompA)
- expectType<number>(CompA.size)
- expectType<SizeType>(CompA.$props.size)
- })
- describe('withKeys and withModifiers as pro', () => {
- const onKeydown = withKeys(e => {}, [''])
- const onClick = withModifiers(e => {}, [])
- ;<input onKeydown={onKeydown} onClick={onClick} />
- })
- // #3367 expose components types
- describe('expose component types', () => {
- const child = defineComponent({
- props: {
- a: String,
- },
- })
- const parent = defineComponent({
- components: {
- child,
- child2: {
- template: `<div></div>`,
- },
- },
- })
- expectType<typeof child>(parent.components!.child)
- expectType<Component>(parent.components!.child2)
- // global components
- expectType<Readonly<KeepAliveProps>>(
- new parent.components!.KeepAlive().$props,
- )
- expectType<Readonly<KeepAliveProps>>(new child.components!.KeepAlive().$props)
- // runtime-dom components
- expectType<Readonly<TransitionProps>>(
- new parent.components!.Transition().$props,
- )
- expectType<Readonly<TransitionProps>>(
- new child.components!.Transition().$props,
- )
- })
- describe('directive typing', () => {
- const customDirective: Directive = {
- created(_) {},
- }
- const comp = defineComponent({
- props: {
- a: String,
- },
- directives: {
- customDirective,
- localDirective: {
- created(_, { arg }) {
- expectType<string | undefined>(arg)
- },
- },
- },
- })
- expectType<typeof customDirective>(comp.directives!.customDirective)
- expectType<Directive>(comp.directives!.localDirective)
- // global directive
- expectType<typeof vShow>(comp.directives!.vShow)
- })
- describe('expose typing', () => {
- const Comp = defineComponent({
- expose: ['a', 'b'],
- props: {
- some: String,
- },
- data() {
- return { a: 1, b: '2', c: 1 }
- },
- })
- expectType<Array<'a' | 'b'>>(Comp.expose!)
- const vm = new Comp()
- // internal should still be exposed
- vm.$props
- expectType<number>(vm.a)
- expectType<string>(vm.b)
- // @ts-expect-error shouldn't be exposed
- vm.c
- })
- import type {
- AllowedComponentProps,
- ComponentCustomProps,
- ComponentInstance,
- ComponentOptionsMixin,
- DefineComponent,
- Directive,
- EmitsOptions,
- ExtractPropTypes,
- KeepAliveProps,
- TransitionProps,
- VNodeProps,
- vShow,
- } from 'vue'
- // code generated by tsc / vue-tsc, make sure this continues to work
- // so we don't accidentally change the args order of DefineComponent
- declare const MyButton: DefineComponent<
- {},
- () => JSX.Element,
- {},
- {},
- {},
- ComponentOptionsMixin,
- ComponentOptionsMixin,
- EmitsOptions,
- string,
- VNodeProps & AllowedComponentProps & ComponentCustomProps,
- Readonly<ExtractPropTypes<{}>>,
- {},
- {}
- >
- ;<MyButton class="x" />
- describe('__typeProps backdoor for union type for conditional props', () => {
- interface CommonProps {
- size?: 'xl' | 'l' | 'm' | 's' | 'xs'
- }
- type ConditionalProps =
- | {
- color?: 'normal' | 'primary' | 'secondary'
- appearance?: 'normal' | 'outline' | 'text'
- }
- | {
- color: 'white'
- appearance: 'outline'
- }
- type Props = CommonProps & ConditionalProps
- const Comp = defineComponent({
- __typeProps: {} as Props,
- })
- // @ts-expect-error
- ;<Comp color="white" />
- // @ts-expect-error
- ;<Comp color="white" appearance="normal" />
- ;<Comp color="white" appearance="outline" />
- const c = new Comp()
- // @ts-expect-error
- c.$props = { color: 'white' }
- // @ts-expect-error
- c.$props = { color: 'white', appearance: 'text' }
- c.$props = { color: 'white', appearance: 'outline' }
- })
- describe('__typeEmits backdoor, 3.3+ object syntax', () => {
- type Emits = {
- change: [id: number]
- update: [value: string]
- }
- const Comp = defineComponent({
- __typeEmits: {} as Emits,
- mounted() {
- this.$props.onChange?.(123)
- // @ts-expect-error
- this.$props.onChange?.('123')
- this.$props.onUpdate?.('foo')
- // @ts-expect-error
- this.$props.onUpdate?.(123)
- // @ts-expect-error
- this.$emit('foo')
- this.$emit('change', 123)
- // @ts-expect-error
- this.$emit('change', '123')
- this.$emit('update', 'test')
- // @ts-expect-error
- this.$emit('update', 123)
- },
- })
- ;<Comp onChange={id => id.toFixed(2)} />
- ;<Comp onUpdate={id => id.toUpperCase()} />
- // @ts-expect-error
- ;<Comp onChange={id => id.slice(1)} />
- // @ts-expect-error
- ;<Comp onUpdate={id => id.toFixed(2)} />
- const c = new Comp()
- // @ts-expect-error
- c.$emit('foo')
- c.$emit('change', 123)
- // @ts-expect-error
- c.$emit('change', '123')
- c.$emit('update', 'test')
- // @ts-expect-error
- c.$emit('update', 123)
- })
- describe('__typeEmits backdoor, call signature syntax', () => {
- type Emits = {
- (e: 'change', id: number): void
- (e: 'update', value: string): void
- }
- const Comp = defineComponent({
- __typeEmits: {} as Emits,
- mounted() {
- this.$props.onChange?.(123)
- // @ts-expect-error
- this.$props.onChange?.('123')
- this.$props.onUpdate?.('foo')
- // @ts-expect-error
- this.$props.onUpdate?.(123)
- // @ts-expect-error
- this.$emit('foo')
- this.$emit('change', 123)
- // @ts-expect-error
- this.$emit('change', '123')
- this.$emit('update', 'test')
- // @ts-expect-error
- this.$emit('update', 123)
- },
- })
- ;<Comp onChange={id => id.toFixed(2)} />
- ;<Comp onUpdate={id => id.toUpperCase()} />
- // @ts-expect-error
- ;<Comp onChange={id => id.slice(1)} />
- // @ts-expect-error
- ;<Comp onUpdate={id => id.toFixed(2)} />
- const c = new Comp()
- // @ts-expect-error
- c.$emit('foo')
- c.$emit('change', 123)
- // @ts-expect-error
- c.$emit('change', '123')
- c.$emit('update', 'test')
- // @ts-expect-error
- c.$emit('update', 123)
- })
- describe('__typeRefs backdoor, object syntax', () => {
- type Refs = {
- foo: number
- }
- const Parent = defineComponent({
- __typeRefs: {} as { child: ComponentInstance<typeof Child> },
- })
- const Child = defineComponent({
- __typeRefs: {} as Refs,
- })
- const c = new Parent()
- const refs = c.$refs
- expectType<ComponentInstance<typeof Child>>(refs.child)
- expectType<number>(refs.child.$refs.foo)
- })
- describe('__typeEl backdoor', () => {
- const Comp = defineComponent({
- __typeEl: {} as HTMLAnchorElement,
- })
- const c = new Comp()
- expectType<HTMLAnchorElement>(c.$el)
- })
- defineComponent({
- props: {
- foo: [String, null],
- },
- setup(props) {
- expectType<IsAny<typeof props.foo>>(false)
- expectType<string | null | undefined>(props.foo)
- },
- })
- import type * as vue from 'vue'
- interface ErrorMessageSlotProps {
- message: string | undefined
- }
- /**
- * #10842
- * component types generated by vue-tsc
- * relying on legacy CreateComponentPublicInstance signature
- */
- declare const ErrorMessage: {
- new (...args: any[]): vue.CreateComponentPublicInstance<
- Readonly<
- vue.ExtractPropTypes<{
- as: {
- type: StringConstructor
- default: any
- }
- name: {
- type: StringConstructor
- required: true
- }
- }>
- >,
- () =>
- | VNode<
- vue.RendererNode,
- vue.RendererElement,
- {
- [key: string]: any
- }
- >
- | vue.Slot<any>
- | VNode<
- vue.RendererNode,
- vue.RendererElement,
- {
- [key: string]: any
- }
- >[]
- | {
- default: () => VNode<
- vue.RendererNode,
- vue.RendererElement,
- {
- [key: string]: any
- }
- >[]
- },
- unknown,
- {},
- {},
- vue.ComponentOptionsMixin,
- vue.ComponentOptionsMixin,
- {},
- vue.VNodeProps &
- vue.AllowedComponentProps &
- vue.ComponentCustomProps &
- Readonly<
- vue.ExtractPropTypes<{
- as: {
- type: StringConstructor
- default: any
- }
- name: {
- type: StringConstructor
- required: true
- }
- }>
- >,
- {
- as: string
- },
- true,
- {},
- {},
- {
- P: {}
- B: {}
- D: {}
- C: {}
- M: {}
- Defaults: {}
- },
- Readonly<
- vue.ExtractPropTypes<{
- as: {
- type: StringConstructor
- default: any
- }
- name: {
- type: StringConstructor
- required: true
- }
- }>
- >,
- () =>
- | VNode<
- vue.RendererNode,
- vue.RendererElement,
- {
- [key: string]: any
- }
- >
- | vue.Slot<any>
- | VNode<
- vue.RendererNode,
- vue.RendererElement,
- {
- [key: string]: any
- }
- >[]
- | {
- default: () => VNode<
- vue.RendererNode,
- vue.RendererElement,
- {
- [key: string]: any
- }
- >[]
- },
- {},
- {},
- {},
- {
- as: string
- }
- >
- __isFragment?: never
- __isTeleport?: never
- __isSuspense?: never
- } & vue.ComponentOptionsBase<
- Readonly<
- vue.ExtractPropTypes<{
- as: {
- type: StringConstructor
- default: any
- }
- name: {
- type: StringConstructor
- required: true
- }
- }>
- >,
- () =>
- | VNode<
- vue.RendererNode,
- vue.RendererElement,
- {
- [key: string]: any
- }
- >
- | vue.Slot<any>
- | VNode<
- vue.RendererNode,
- vue.RendererElement,
- {
- [key: string]: any
- }
- >[]
- | {
- default: () => VNode<
- vue.RendererNode,
- vue.RendererElement,
- {
- [key: string]: any
- }
- >[]
- },
- unknown,
- {},
- {},
- vue.ComponentOptionsMixin,
- vue.ComponentOptionsMixin,
- {},
- string,
- {
- as: string
- },
- {},
- string,
- {}
- > &
- vue.VNodeProps &
- vue.AllowedComponentProps &
- vue.ComponentCustomProps &
- (new () => {
- $slots: {
- default: (arg: ErrorMessageSlotProps) => VNode[]
- }
- })
- ;<ErrorMessage name="password" class="error" />
- // #10843
- createApp({}).component(
- 'SomeComponent',
- defineComponent({
- props: {
- title: String,
- },
- setup(props) {
- expectType<string | undefined>(props.title)
- return {}
- },
- }),
- )
- const Comp = defineComponent({
- props: {
- actionText: {
- type: {} as PropType<string>,
- default: 'Become a sponsor',
- },
- },
- __typeProps: {} as {
- actionText?: string
- },
- })
- const instance = new Comp()
- function expectString(s: string) {}
- // instance prop with default should be non-null
- expectString(instance.actionText)
- // public prop on $props should be optional
- // @ts-expect-error
- expectString(instance.$props.actionText)
- // #12122
- defineComponent({
- props: { foo: String },
- render() {
- expectType<{ readonly foo?: string }>(this.$props)
- // @ts-expect-error
- expectType<string>(this.$props)
- },
- })
- // #14117
- defineComponent({
- setup() {
- const setup1 = ref('setup1')
- const setup2 = ref('setup2')
- return { setup1, setup2 }
- },
- data() {
- return {
- data1: 1,
- }
- },
- props: {
- props1: {
- type: String,
- },
- },
- methods: {
- methods1() {
- return `methods1`
- },
- },
- computed: {
- computed1() {
- this.setup1
- this.setup2
- this.data1
- this.props1
- this.methods1()
- return `computed1`
- },
- },
- expose: ['setup1'],
- })
|