Skip to content

docs: warn about Date coercion in CRUD callback inputs#649

Open
MMMikeM wants to merge 1 commit into
LegendApp:mainfrom
MMMikeM:docs/crud-date-field-jsdoc
Open

docs: warn about Date coercion in CRUD callback inputs#649
MMMikeM wants to merge 1 commit into
LegendApp:mainfrom
MMMikeM:docs/crud-date-field-jsdoc

Conversation

@MMMikeM
Copy link
Copy Markdown

@MMMikeM MMMikeM commented Mar 28, 2026

Summary

  • Adds JSDoc to create, update, and delete on SyncedCrudPropsBase warning that fields named by fieldUpdatedAt/fieldCreatedAt will be Date at runtime, not the declared type

clone() uses a JSON reviver that converts ISO 8601 strings to Date objects. When fieldUpdatedAt or fieldCreatedAt is set, the corresponding fields on the callback input parameter are Date at runtime — but the TypeScript types still reflect the original declared type (e.g. string or number). This can cause silent failures when passing those values to APIs that expect strings.

The JSDoc surfaces this in the IDE on hover so consumers are aware of the mismatch.

Test plan

  • No type or runtime changes — JSDoc only

clone()'s JSON reviver silently converts ISO 8601 strings to Date
objects on fields named by fieldUpdatedAt/fieldCreatedAt, but the
callback parameter types still reflect the original declared type.
@MMMikeM
Copy link
Copy Markdown
Author

MMMikeM commented Mar 28, 2026

Reproduction: runtime Date coercion in CRUD callbacks

test('fieldUpdatedAt field is Date at runtime despite string typing', async () => {
    let receivedInput: any;

    const obs = observable(
        syncedCrud({
            initial: { a: { id: 'a', updatedAt: '2024-01-01T00:00:00.000Z' } },
            fieldUpdatedAt: 'updatedAt',
            update: async (input) => { receivedInput = input; },
        }),
    );

    await promiseTimeout(1);
    obs.a.set({ id: 'a', updatedAt: '2025-01-01T00:00:00.000Z' });
    await promiseTimeout(1);

    // TS says input.updatedAt is `string`, but clone()'s JSON reviver converts it
    expect(receivedInput.updatedAt).toBeInstanceOf(Date);
});

This passes — confirming the type/runtime mismatch the JSDoc warns about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant