Skip to content

Commit

Permalink
wip: defineModel
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Apr 5, 2023
1 parent 9557529 commit 221dabc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ type EmitsDeclType = FromNormalScript<
TSFunctionType | TSTypeLiteral | TSInterfaceBody
>

export interface ModelDecl {
type: TSType
option: Node
}

/**
* Compile `<script setup>`
* It requires the whole SFC descriptor because we need to handle and merge
Expand Down Expand Up @@ -325,6 +330,7 @@ export function compileScript(
let emitsTypeDecl: EmitsDeclType | undefined
let emitIdentifier: string | undefined
let optionsRuntimeDecl: Node | undefined
// let modelDeclMap: Record<string, ModelDecl> = {}
let hasAwait = false
let hasInlinedSsrRenderFn = false
// props/emits declared via types
Expand Down
25 changes: 24 additions & 1 deletion packages/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
withDefaults,
Slots,
defineSlots,
VNode
VNode,
WritableComputedRef,
defineModel
} from 'vue'
import { describe, expectType } from './utils'

Expand Down Expand Up @@ -202,6 +204,27 @@ describe('defineSlots', () => {
expectType<Slots>(slotsUntype)
})

describe('defineModel', () => {
const modelValue = defineModel<string>()
expectType<WritableComputedRef<string | undefined>>(modelValue)
modelValue.value = 'new value'

const modelValueRequired = defineModel<boolean>({ required: true })
expectType<WritableComputedRef<boolean>>(modelValueRequired)

const modelValueDefault = defineModel<boolean>({ default: true })
expectType<WritableComputedRef<boolean>>(modelValueDefault)

const count = defineModel<number>('count')
expectType<WritableComputedRef<number | undefined>>(count)

const countRequired = defineModel<number>('count', { required: true })
expectType<WritableComputedRef<number>>(countRequired)

const countDefault = defineModel<number>('count', { default: 1 })
expectType<WritableComputedRef<number>>(countDefault)
})

describe('useAttrs', () => {
const attrs = useAttrs()
expectType<Record<string, unknown>>(attrs)
Expand Down
33 changes: 31 additions & 2 deletions packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from './componentProps'
import { warn } from './warning'
import { SlotsType, TypedSlots } from './componentSlots'
import { WritableComputedRef } from '@vue/reactivity'

// dev only
const warnRuntimeUsage = (method: string) =>
Expand Down Expand Up @@ -200,11 +201,39 @@ export function defineOptions<

export function defineSlots<
S extends Record<string, any> = Record<string, any>
>(): // @ts-expect-error
TypedSlots<SlotsType<S>> {
>(): TypedSlots<SlotsType<S>> {
if (__DEV__) {
warnRuntimeUsage(`defineSlots`)
}
return null as any
}

export function defineModel<T>(
options: { required: true } & Record<string, unknown>
): WritableComputedRef<T>
export function defineModel<T>(
options: { default: any } & Record<string, unknown>
): WritableComputedRef<T>
export function defineModel<T>(
options?: Record<string, unknown>
): WritableComputedRef<T | undefined>
export function defineModel<T>(
name: string,
options: { required: true } & Record<string, unknown>
): WritableComputedRef<T>
export function defineModel<T>(
name: string,
options: { default: any } & Record<string, unknown>
): WritableComputedRef<T>
export function defineModel<T>(
name: string,
options?: Record<string, unknown>
): WritableComputedRef<T | undefined>
export function defineModel() {
if (__DEV__) {
warnRuntimeUsage('defineModel')
}
return null as any
}

type NotUndefined<T> = T extends undefined ? never : T
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export {
defineExpose,
defineOptions,
defineSlots,
defineModel,
withDefaults,
// internal
mergeDefaults,
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/types/scriptSetupHelpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type _defineEmits = typeof defineEmits
type _defineExpose = typeof defineExpose
type _defineOptions = typeof defineOptions
type _defineSlots = typeof defineSlots
type _defineModel = typeof defineModel
type _withDefaults = typeof withDefaults

declare global {
Expand All @@ -13,5 +14,6 @@ declare global {
const defineExpose: _defineExpose
const defineOptions: _defineOptions
const defineSlots: _defineSlots
const defineModel: _defineModel
const withDefaults: _withDefaults
}

0 comments on commit 221dabc

Please sign in to comment.