Skip to content

Commit

Permalink
Resolve type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gwbaik9717 committed Sep 8, 2024
1 parent 7048002 commit 3806420
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 80 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/src/document/change/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class ChangeContext<P extends Indexable = Indexable> {

const reversePresence: Partial<P> = {};
for (const key of this.reversePresenceKeys) {
reversePresence[key as keyof P] = this.previousPresence[key];
reversePresence[key as keyof P] = this.previousPresence[key as keyof P];
}
return reversePresence;
}
Expand Down
136 changes: 68 additions & 68 deletions packages/sdk/src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ type Json = JsonScalar | JsonArray | JsonObject;

// eslint-disable-next-line @typescript-eslint/ban-types
type JsonScalar = string | number | boolean | null;
type JsonArray = Json[];
type JsonArray = Array<Json>;
type JsonObject = { [key: string]: Json | undefined };

/**
Expand All @@ -445,14 +445,14 @@ export type DocumentKey = string;
type OperationInfoOfElement<TElement> = TElement extends Text
? TextOperationInfo
: TElement extends Counter
? CounterOperationInfo
: TElement extends Tree
? TreeOperationInfo
: TElement extends BaseArray<any>
? ArrayOperationInfo
: TElement extends BaseObject<any>
? ObjectOperationInfo
: OperationInfo;
? CounterOperationInfo
: TElement extends Tree
? TreeOperationInfo
: TElement extends BaseArray<any>
? ArrayOperationInfo
: TElement extends BaseObject<any>
? ObjectOperationInfo
: OperationInfo;

/**
* `OperationInfoOfInternal` represents the type of the operation info of the
Expand All @@ -473,49 +473,49 @@ type OperationInfoOfInternal<
> = TDepth extends 0
? TElement
: TKeyOrPath extends `${infer TFirst}.${infer TRest}`
? TFirst extends keyof TElement
? TElement[TFirst] extends BaseArray<unknown>
? OperationInfoOfInternal<
TElement[TFirst],
number,
DecreasedDepthOf<TDepth>
>
: OperationInfoOfInternal<
TElement[TFirst],
TRest,
DecreasedDepthOf<TDepth>
>
: OperationInfo
: TKeyOrPath extends keyof TElement
? TElement[TKeyOrPath] extends BaseArray<unknown>
? ArrayOperationInfo
: OperationInfoOfElement<TElement[TKeyOrPath]>
: OperationInfo;
? TFirst extends keyof TElement
? TElement[TFirst] extends BaseArray<unknown>
? OperationInfoOfInternal<
TElement[TFirst],
number,
DecreasedDepthOf<TDepth>
>
: OperationInfoOfInternal<
TElement[TFirst],
TRest,
DecreasedDepthOf<TDepth>
>
: OperationInfo
: TKeyOrPath extends keyof TElement
? TElement[TKeyOrPath] extends BaseArray<unknown>
? ArrayOperationInfo
: OperationInfoOfElement<TElement[TKeyOrPath]>
: OperationInfo;

/**
* `DecreasedDepthOf` represents the type of the decreased depth of the given depth.
*/
type DecreasedDepthOf<Depth extends number = 0> = Depth extends 10
? 9
: Depth extends 9
? 8
: Depth extends 8
? 7
: Depth extends 7
? 6
: Depth extends 6
? 5
: Depth extends 5
? 4
: Depth extends 4
? 3
: Depth extends 3
? 2
: Depth extends 2
? 1
: Depth extends 1
? 0
: -1;
? 8
: Depth extends 8
? 7
: Depth extends 7
? 6
: Depth extends 6
? 5
: Depth extends 5
? 4
: Depth extends 4
? 3
: Depth extends 3
? 2
: Depth extends 2
? 1
: Depth extends 1
? 0
: -1;

/**
* `PathOfInternal` represents the type of the path of the given element.
Expand All @@ -527,29 +527,29 @@ type PathOfInternal<
> = Depth extends 0
? Prefix
: TElement extends Record<string, any>
? {
[TKey in keyof TElement]: TElement[TKey] extends LeafElement
? `${Prefix}${TKey & string}`
: TElement[TKey] extends BaseArray<infer TArrayElement>
?
| `${Prefix}${TKey & string}`
| `${Prefix}${TKey & string}.${number}`
| PathOfInternal<
TArrayElement,
`${Prefix}${TKey & string}.${number}.`,
DecreasedDepthOf<Depth>
>
:
| `${Prefix}${TKey & string}`
| PathOfInternal<
TElement[TKey],
`${Prefix}${TKey & string}.`,
DecreasedDepthOf<Depth>
>;
}[keyof TElement]
: Prefix extends `${infer TRest}.`
? TRest
: Prefix;
? {
[TKey in keyof TElement]: TElement[TKey] extends LeafElement
? `${Prefix}${TKey & string}`
: TElement[TKey] extends BaseArray<infer TArrayElement>
?
| `${Prefix}${TKey & string}`
| `${Prefix}${TKey & string}.${number}`
| PathOfInternal<
TArrayElement,
`${Prefix}${TKey & string}.${number}.`,
DecreasedDepthOf<Depth>
>
:
| `${Prefix}${TKey & string}`
| PathOfInternal<
TElement[TKey],
`${Prefix}${TKey & string}.`,
DecreasedDepthOf<Depth>
>;
}[keyof TElement]
: Prefix extends `${infer TRest}.`
? TRest
: Prefix;

/**
* `OperationInfoOf` represents the type of the operation info of the given
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/src/document/json/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ function buildDescendants(
let attrs;

if (typeof attributes === 'object' && !isEmpty(attributes)) {
attributes = stringifyObjectValues(attributes);
const stringifiedAttributes = stringifyObjectValues(attributes);
attrs = new RHT();

for (const [key, value] of Object.entries(attributes)) {
for (const [key, value] of Object.entries(stringifiedAttributes)) {
attrs.set(key, value, ticket);
}
}
Expand Down Expand Up @@ -125,10 +125,10 @@ function createCRDTTreeNode(context: ChangeContext, content: TreeNode) {
let attrs;

if (typeof attributes === 'object' && !isEmpty(attributes)) {
attributes = stringifyObjectValues(attributes);
const stringifiedAttributes = stringifyObjectValues(attributes);
attrs = new RHT();

for (const [key, value] of Object.entries(attributes)) {
for (const [key, value] of Object.entries(stringifiedAttributes)) {
attrs.set(key, value, ticket);
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/sdk/src/util/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { Indexable } from '@yorkie-js-sdk/src/document/document';

/**
* `deepcopy` returns a deep copy of the given object.
*/
Expand All @@ -40,7 +42,7 @@ export const isEmpty = (object: object) => {
/**
* `stringifyObjectValues` makes values of attributes to JSON parsable string.
*/
export const stringifyObjectValues = <A extends object>(
export const stringifyObjectValues = <A extends Indexable>(
attributes: A,
): Record<string, string> => {
const attrs: Record<string, string> = {};
Expand All @@ -53,7 +55,7 @@ export const stringifyObjectValues = <A extends object>(
/**
`parseObjectValues` returns the JSON parsable string values to the origin states.
*/
export const parseObjectValues = <A extends object>(
export const parseObjectValues = <A extends Indexable>(
attrs: Record<string, string>,
): A => {
const attributes: Record<string, unknown> = {};
Expand Down
13 changes: 8 additions & 5 deletions packages/sdk/test/integration/integration_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ export function toDocKey(title: string): string {
.replace(/[^a-z0-9-]/g, '-');
}

export async function withTwoClientsAndDocuments<T>(
export async function withTwoClientsAndDocuments<
T,
P extends Indexable = Indexable,
>(
callback: (
c1: Client,
d1: Document<T>,
d1: Document<T, P>,
c2: Client,
d2: Document<T>,
d2: Document<T, P>,
) => Promise<void>,
title: string,
syncMode: SyncMode = SyncMode.Manual,
Expand All @@ -29,8 +32,8 @@ export async function withTwoClientsAndDocuments<T>(
await client2.activate();

const docKey = `${toDocKey(title)}-${new Date().getTime()}`;
const doc1 = new yorkie.Document<T>(docKey);
const doc2 = new yorkie.Document<T>(docKey);
const doc1 = new yorkie.Document<T, P>(docKey);
const doc2 = new yorkie.Document<T, P>(docKey);

await client1.attach(doc1, { syncMode });
await client2.attach(doc2, { syncMode });
Expand Down

0 comments on commit 3806420

Please sign in to comment.