Skip to content

Commit

Permalink
fix: refine ts type; reject invalid operations (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n authored Apr 29, 2024
1 parent ca5f762 commit 2918d01
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions crates/loro-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2199,6 +2199,7 @@ impl LoroList {
}

/// Push a value to the end of the list.
#[wasm_bindgen(skip_typescript)]
pub fn push(&self, value: JsLoroValue) -> JsResult<()> {
let v: JsValue = value.into();
self.handler.push(v.into())?;
Expand Down Expand Up @@ -2563,6 +2564,7 @@ impl LoroMovableList {
}

/// Push a value to the end of the list.
#[wasm_bindgen(skip_typescript)]
pub fn push(&self, value: JsLoroValue) -> JsResult<()> {
let v: JsValue = value.into();
self.handler.push(v.into())?;
Expand Down
13 changes: 9 additions & 4 deletions loro-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,9 @@ declare module "loro-wasm" {
* console.log(list.value); // [100, "foo", true];
* ```
*/
insert(pos: number, value: Exclude<T, Container>): void;
insert<V extends T>(pos: number, value: Exclude<V, Container>): void;
delete(pos: number, len: number): void;
push<V extends T>(value: Exclude<V, Container>): void;
subscribe(listener: Listener): number;
getAttached(): undefined | LoroList<T>;
}
Expand Down Expand Up @@ -388,8 +389,9 @@ declare module "loro-wasm" {
* console.log(list.value); // [100, "foo", true];
* ```
*/
insert(pos: number, value: Exclude<T, Container>): void;
insert<V extends T>(pos: number, value: Exclude<V, Container>): void;
delete(pos: number, len: number): void;
push<V extends T>(value: Exclude<V, Container>): void;
subscribe(listener: Listener): number;
getAttached(): undefined | LoroMovableList<T>;
/**
Expand All @@ -416,7 +418,7 @@ declare module "loro-wasm" {
* console.log(list.value); // [100, "bar", true];
* ```
*/
set(pos: number, value: Exclude<T, Container>): void;
set<V extends T>(pos: number, value: Exclude<V, Container>): void;
/**
* Set a container at the index.
*
Expand Down Expand Up @@ -510,7 +512,10 @@ declare module "loro-wasm" {
* map.set("foo", "baz");
* ```
*/
set<Key extends keyof T>(key: Key, value: Exclude<T[Key], Container>): void;
set<Key extends keyof T, V extends T[Key]>(
key: Key,
value: Exclude<V, Container>,
): void;
delete(key: string): void;
subscribe(listener: Listener): number;
}
Expand Down
14 changes: 14 additions & 0 deletions loro-js/tests/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Loro,
LoroList,
LoroMap,
LoroMovableList,
LoroText,
LoroTree,
PeerID,
Expand Down Expand Up @@ -63,3 +64,16 @@ test("doc type and container type", () => {
const numList = map.setContainer("num", new LoroList());
expectTypeOf(list.toArray()).toMatchTypeOf<string[]>();
});

test("fail on set/insert container", () => {
const list = new LoroList();
// list.insert(0, list); // should fail
const map = new LoroMap();
// map.set("a", list); // should fail
map.setContainer("a", list); // should work
const mList = new LoroMovableList();
// mList.insert(0, list); // should fail
// mList.set(0, list); // should fail
mList.insertContainer(0, list); // should work
mList.setContainer(0, list); // should work
});

0 comments on commit 2918d01

Please sign in to comment.