From ae0e42e27645a4420c4018c7d4577ae786f7da4f Mon Sep 17 00:00:00 2001 From: Youngteac Hong Date: Wed, 23 Oct 2024 15:00:49 +0900 Subject: [PATCH] Revise the codes --- packages/sdk/src/client/client.ts | 11 ++++++----- packages/sdk/test/integration/document_test.ts | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/sdk/src/client/client.ts b/packages/sdk/src/client/client.ts index c61ec8fae..a678f10aa 100644 --- a/packages/sdk/src/client/client.ts +++ b/packages/sdk/src/client/client.ts @@ -296,8 +296,8 @@ export class Client { public attach( doc: Document, options: { - initialPresence?: P; initialRoot?: T; + initialPresence?: P; syncMode?: SyncMode; } = {}, ): Promise> { @@ -369,11 +369,12 @@ export class Client { const crdtObject = doc.getRootObject(); if (options.initialRoot) { + const initialRoot = options.initialRoot; doc.update((root) => { - for (const [k, v] of Object.entries(options.initialRoot!)) { - if (!crdtObject.get(k)) { - // TODO(raararaara): Need a way to accurately infer the type of `k` for indexing. - (root as Record)[k] = v; + for (const [k, v] of Object.entries(initialRoot)) { + if (!crdtObject.has(k)) { + const key = k as keyof T; + root[key] = v as any; } } }); diff --git a/packages/sdk/test/integration/document_test.ts b/packages/sdk/test/integration/document_test.ts index eb2ea11cb..b3df7b787 100644 --- a/packages/sdk/test/integration/document_test.ts +++ b/packages/sdk/test/integration/document_test.ts @@ -1259,7 +1259,7 @@ describe('Document', function () { `${task.name}-${name}-${new Date().getTime()}`, ); - type docType = { + type DocType = { tree?: Tree; text?: Text; counter?: Counter; @@ -1267,12 +1267,12 @@ describe('Document', function () { boolean?: boolean; number?: number; long?: Long; - object?: Object; + object?: { k: string }; array?: Array; bytes?: Uint8Array; // date: Date; }; - const doc = new yorkie.Document(docKey); + const doc = new yorkie.Document(docKey); await c1.attach(doc, { initialRoot: {