Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(types): fix shallowRef type error #13135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Alfred-Skyblue wants to merge 2 commits into vuejs:main
base: main
Choose a base branch
Loading
from Alfred-Skyblue:fix/types/shallowRef
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/v3/reactivity/ref.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ declare const ShallowRefMarker: unique symbol

export type ShallowRef<T = any> = Ref<T> & { [ShallowRefMarker]?: true }

export function shallowRef<T>(value: T | Ref<T>): Ref<T> | ShallowRef<T>
export function shallowRef<T extends Ref>(value: T): T
export function shallowRef<T>(value: T): ShallowRef<T>
export function shallowRef<T>(
value: T
): Ref extends T
? T extends Ref
? IfAny<T, ShallowRef<T>, T>
: ShallowRef<T>
: ShallowRef<T>
export function shallowRef<T = any>(): ShallowRef<T | undefined>
export function shallowRef(value?: unknown) {
return createRef(value, true)
Expand Down
17 changes: 13 additions & 4 deletions types/test/v3/reactivity-test.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
markRaw,
shallowReadonly,
set,
del
del,
ShallowRef
} from '../../index'
import { IsUnion, describe, expectType } from '../utils'
import { IsUnion, describe, expectType, expectError } from '../utils'

function plainType(arg: number | Ref<number>) {
// ref coercing
Expand Down Expand Up @@ -163,6 +164,15 @@ if (shallowStatus.value === 'initial') {
shallowStatus.value = 'invalidating'
}

{
// should return ShallowRef<T> | Ref<T>, not ShallowRef<T | Ref<T>>
expectType<ShallowRef<{ name: string }> | Ref<{ name: string }>>(
shallowRef({} as { name: string } | Ref<{ name: string }>)
)
expectType<ShallowRef<number> | Ref<string[]> | ShallowRef<string>>(
shallowRef('' as Ref<string[]> | string | number)
)
}
const refStatus = ref<Status>('initial')
if (refStatus.value === 'initial') {
expectType<Ref<Status>>(shallowStatus)
Expand Down Expand Up @@ -386,7 +396,6 @@ describe('set/del', () => {
del([], 'fse', 123)
})


{
//#12978
type Steps = { step: '1' } | { step: '2' }
Expand All @@ -395,4 +404,4 @@ describe('set/del', () => {

expectType<IsUnion<typeof shallowUnionGenParam>>(false)
expectType<IsUnion<typeof shallowUnionAsCast>>(false)
}
}

AltStyle によって変換されたページ (->オリジナル) /