forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(types): should unwrap tuple correctly (vuejs#3820)
fix vuejs#3819
- Loading branch information
Showing
2 changed files
with
78 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,73 @@ | ||
import { | ||
ref, | ||
readonly, | ||
shallowReadonly, | ||
describe, | ||
expectError, | ||
expectType, | ||
Ref, | ||
reactive, | ||
markRaw | ||
} from './index' | ||
|
||
describe('should support DeepReadonly', () => { | ||
const r = readonly({ obj: { k: 'v' } }) | ||
// @ts-expect-error | ||
expectError((r.obj = {})) | ||
// @ts-expect-error | ||
expectError((r.obj.k = 'x')) | ||
}) | ||
|
||
// #4180 | ||
describe('readonly ref', () => { | ||
const r = readonly(ref({ count: 1 })) | ||
expectType<Ref>(r) | ||
}) | ||
|
||
describe('should support markRaw', () => { | ||
class Test<T> { | ||
item = {} as Ref<T> | ||
} | ||
const test = new Test<number>() | ||
const plain = { | ||
ref: ref(1) | ||
} | ||
|
||
const r = reactive({ | ||
class: { | ||
raw: markRaw(test), | ||
reactive: test | ||
}, | ||
plain: { | ||
raw: markRaw(plain), | ||
reactive: plain | ||
} | ||
}) | ||
|
||
expectType<Test<number>>(r.class.raw) | ||
// @ts-expect-error it should unwrap | ||
expectType<Test<number>>(r.class.reactive) | ||
|
||
expectType<Ref<number>>(r.plain.raw.ref) | ||
// @ts-expect-error it should unwrap | ||
expectType<Ref<number>>(r.plain.reactive.ref) | ||
}) | ||
|
||
describe('shallowReadonly ref unwrap', () => { | ||
const r = shallowReadonly({ count: { n: ref(1) } }) | ||
// @ts-expect-error | ||
r.count = 2 | ||
expectType<Ref>(r.count.n) | ||
r.count.n.value = 123 | ||
}) | ||
import { | ||
ref, | ||
readonly, | ||
shallowReadonly, | ||
describe, | ||
expectError, | ||
expectType, | ||
Ref, | ||
reactive, | ||
markRaw | ||
} from './index' | ||
|
||
describe('should support DeepReadonly', () => { | ||
const r = readonly({ obj: { k: 'v' } }) | ||
// @ts-expect-error | ||
expectError((r.obj = {})) | ||
// @ts-expect-error | ||
expectError((r.obj.k = 'x')) | ||
}) | ||
|
||
// #4180 | ||
describe('readonly ref', () => { | ||
const r = readonly(ref({ count: 1 })) | ||
expectType<Ref>(r) | ||
}) | ||
|
||
describe('should support markRaw', () => { | ||
class Test<T> { | ||
item = {} as Ref<T> | ||
} | ||
const test = new Test<number>() | ||
const plain = { | ||
ref: ref(1) | ||
} | ||
|
||
const r = reactive({ | ||
class: { | ||
raw: markRaw(test), | ||
reactive: test | ||
}, | ||
plain: { | ||
raw: markRaw(plain), | ||
reactive: plain | ||
} | ||
}) | ||
|
||
expectType<Test<number>>(r.class.raw) | ||
// @ts-expect-error it should unwrap | ||
expectType<Test<number>>(r.class.reactive) | ||
|
||
expectType<Ref<number>>(r.plain.raw.ref) | ||
// @ts-expect-error it should unwrap | ||
expectType<Ref<number>>(r.plain.reactive.ref) | ||
}) | ||
|
||
describe('shallowReadonly ref unwrap', () => { | ||
const r = shallowReadonly({ count: { n: ref(1) } }) | ||
// @ts-expect-error | ||
r.count = 2 | ||
expectType<Ref>(r.count.n) | ||
r.count.n.value = 123 | ||
}) | ||
|
||
// #3819 | ||
describe('should unwrap tuple correctly', () => { | ||
const readonlyTuple = [ref(0)] as const | ||
const reactiveReadonlyTuple = reactive(readonlyTuple) | ||
expectType<Ref<number>>(reactiveReadonlyTuple[0]) | ||
|
||
const tuple: [Ref<number>] = [ref(0)] | ||
const reactiveTuple = reactive(tuple) | ||
expectType<Ref<number>>(reactiveTuple[0]) | ||
}) |