Skip to content
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

fix(current): fix current() type and castMutable() APIs #62

Merged
merged 2 commits into from
Nov 22, 2024
Merged
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
11 changes: 7 additions & 4 deletions src/current.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DraftType, ProxyDraft } from './interface';
import { type Draft, DraftType, type ProxyDraft } from './interface';
import {
forEach,
get,
Expand Down Expand Up @@ -70,8 +70,8 @@ function getCurrent(target: any) {
type === DraftType.Map
? new Map(target)
: type === DraftType.Set
? Array.from(proxyDraft!.setMap!.values()!)
: shallowCopy(target, proxyDraft?.options);
? Array.from(proxyDraft!.setMap!.values()!)
: shallowCopy(target, proxyDraft?.options);
}

if (proxyDraft) {
Expand Down Expand Up @@ -117,7 +117,10 @@ function getCurrent(target: any) {
* );
* ```
*/
export function current<T extends object>(target: T): T {
export function current<T extends object>(target: Draft<T>): T;
/** @deprecated You should call current only on `Draft<T>` types. */
export function current<T extends object>(target: T): T;
export function current<T extends object>(target: T | Draft<T>): T {
if (!isDraft(target)) {
throw new Error(`current() is only used for Draft, parameter: ${target}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { isDraft } from './utils/draft';
export { isDraftable } from './utils/draft';
export { markSimpleObject } from './utils/marker';

export { castDraft, castImmutable } from './utils/cast';
export { castDraft, castImmutable, castMutable } from './utils/cast';
export type {
Immutable,
Draft,
Expand Down
7 changes: 7 additions & 0 deletions src/utils/cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ export function castDraft<T>(value: T): Draft<T> {
export function castImmutable<T>(value: T): Immutable<T> {
return value as any;
}

/**
* Cast a value to an Mutable type value.
*/
export function castMutable<T>(draft: Draft<T>): T {
return draft as any;
}
10 changes: 10 additions & 0 deletions test/current.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,13 @@ test('nested create() - Avoid deep copies', () => {
});
});
});


test('#61 - type issue: current of Draft<T> type should return T type', () => {
function test<T extends { x: { y: ReadonlySet<string> } }>(base: T): T {
const [draft] = create(base);
const currentValue0 = current(draft); // Type Draft<T> is assignable to type T
const currentValue1: T = current(base); // T is assignable to type T
return currentValue0;
}
});
28 changes: 28 additions & 0 deletions test/immer-non-support.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-inner-declarations */
/* eslint-disable symbol-description */
/* eslint-disable no-unused-expressions */
Expand All @@ -15,6 +16,8 @@ import {
applyPatches,
setUseStrictShallowCopy,
current as immerCurrent,
createDraft,
finishDraft,
} from 'immer';
import { create, apply, current } from '../src';

Expand Down Expand Up @@ -589,3 +592,28 @@ test('#47 Avoid deep copies', () => {
});
}
});

test('#61 - type issue: current of Draft<T> type should return T type', () => {
{
function test<T extends { x: { y: ReadonlySet<string> } }>(base: T): T {
const draft = createDraft(base);
// @ts-ignore
const currentValue: T = immerCurrent(draft); // !!! Type Draft<T> is not assignable to type T
// @ts-expect-error
return finishDraft(draft);
}
expect(test({ x: { y: new Set(['a', 'b']) } })).toEqual({
x: { y: new Set(['a', 'b']) },
});
}
{
function test<T extends { x: { y: ReadonlySet<string> } }>(base: T): T {
const [draft, f] = create(base);
const currentValue: T = current(draft);
return f();
}
expect(test({ x: { y: new Set(['a', 'b']) } })).toEqual({
x: { y: new Set(['a', 'b']) },
});
}
});
20 changes: 20 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable prefer-template */
/* eslint-disable no-unused-expressions */
/* eslint-disable arrow-body-style */
Expand All @@ -14,6 +15,7 @@ import {
markSimpleObject,
rawReturn,
makeCreator,
castMutable,
} from '../src';
import { PROXY_DRAFT } from '../src/constant';

Expand Down Expand Up @@ -4083,3 +4085,21 @@ test('#59 - Failure to apply inverse patchset(Map)', () => {
const reverted2 = apply(myObj, patchset);
expect(reverted2).toEqual(newState);
});

test('#61 - type issue: current of Draft<T> type should return T type', () => {
function test<T extends { x: { y: ReadonlySet<string> } }>(base: T): T {
const [draft, f] = create(base);
const mutableValue: T = castMutable(draft);
const currentValue: T = current(draft);
expect(() => {
// @ts-expect-error
const value = current({ x: { y: new Set(['a', 'b']) } } as T);
}).toThrowErrorMatchingInlineSnapshot(
`"current() is only used for Draft, parameter: [object Object]"`
);
return f();
}
expect(test({ x: { y: new Set(['a', 'b']) } })).toEqual({
x: { y: new Set(['a', 'b']) },
});
});
Loading