| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144 |
- 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
- defineComponent(
- (_props: { msg: string }) => {
- return () => {}
- },
- {
- props: ['msg'],
- },
- )
- defineComponent(
- <T extends string>(_props: { msg: T }) => {
- return () => {}
- },
- {
- props: ['msg'],
- },
- )
- defineComponent(
- <T extends string>(_props: { msg: T }) => {
- return () => {}
- },
- {
- props: {
- msg: String,
- },
- },
- )
- // @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,
- },
- },
- )
- // @ts-expect-error prop keys don't match
- defineComponent(
- (_props: { msg: string }, ctx) => {
- return () => {}
- },
- {
- props: {
- msg: String,
- bar: String,
- },
- },
- )
- })
- // 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'],
- })
|