diff --git a/client/packages/admin/src/index.ts b/client/packages/admin/src/index.ts index 29ec457ec..dccc33a72 100644 --- a/client/packages/admin/src/index.ts +++ b/client/packages/admin/src/index.ts @@ -45,6 +45,8 @@ import { type InstaQLEntity, type InstaQLResult, type InstantRules, + type UpdateParams, + type LinkParams, } from "@instantdb/core"; import version from "./version"; @@ -951,4 +953,6 @@ export { type InstaQLEntity, type InstaQLResult, type InstantRules, + type UpdateParams, + type LinkParams, }; diff --git a/client/packages/core/src/index.ts b/client/packages/core/src/index.ts index a47dacced..571e43ded 100644 --- a/client/packages/core/src/index.ts +++ b/client/packages/core/src/index.ts @@ -62,6 +62,8 @@ import type { ValueTypes, InstantUnknownSchema, BackwardsCompatibleSchema, + UpdateParams, + LinkParams, } from "./schemaTypes"; const defaultOpenDevtool = true; @@ -730,4 +732,6 @@ export { type IInstantDatabase, type BackwardsCompatibleSchema, type InstantRules, + type UpdateParams, + type LinkParams, }; diff --git a/client/packages/core/src/instatx.ts b/client/packages/core/src/instatx.ts index 3308a617f..bad7a353a 100644 --- a/client/packages/core/src/instatx.ts +++ b/client/packages/core/src/instatx.ts @@ -1,8 +1,6 @@ import type { - DataAttrDef, IContainEntitiesAndLinks, - InstantGraph, - LinkAttrDef, + LinkParams, UpdateParams, } from "./schemaTypes"; @@ -14,20 +12,6 @@ type LookupRef = [string, any]; type Lookup = string; export type Op = [Action, EType, Id | LookupRef, Args]; -type LinkParams< - Schema extends IContainEntitiesAndLinks, - EntityName extends keyof Schema["entities"], -> = { - [LinkName in keyof Schema["entities"][EntityName]["links"]]?: Schema["entities"][EntityName]["links"][LinkName] extends LinkAttrDef< - infer Cardinality, - any - > - ? Cardinality extends "one" - ? string - : string | string[] - : never; -} & (Schema extends InstantGraph ? {} : { [attribute: string]: any }); - export interface TransactionChunk< Schema extends IContainEntitiesAndLinks, EntityName extends keyof Schema["entities"], diff --git a/client/packages/core/src/schemaTypes.ts b/client/packages/core/src/schemaTypes.ts index 11452a0de..7c9082a8d 100644 --- a/client/packages/core/src/schemaTypes.ts +++ b/client/packages/core/src/schemaTypes.ts @@ -466,3 +466,17 @@ export type UpdateParams< Schema["entities"][EntityName]["attrs"][AttrName] >; }; + +export type LinkParams< + Schema extends IContainEntitiesAndLinks, + EntityName extends keyof Schema["entities"], +> = { + [LinkName in keyof Schema["entities"][EntityName]["links"]]?: Schema["entities"][EntityName]["links"][LinkName] extends LinkAttrDef< + infer Cardinality, + any + > + ? Cardinality extends "one" + ? string + : string | string[] + : never; +}; diff --git a/client/packages/react-native/src/index.ts b/client/packages/react-native/src/index.ts index 2cb57788a..28bad783c 100644 --- a/client/packages/react-native/src/index.ts +++ b/client/packages/react-native/src/index.ts @@ -51,6 +51,8 @@ import { type InstantSchemaDef, type InstantUnknownSchema, type InstantRules, + type UpdateParams, + type LinkParams, } from "@instantdb/core"; /** @@ -147,4 +149,6 @@ export { type InstantUnknownSchema, type BackwardsCompatibleSchema, type InstantRules, + type UpdateParams, + type LinkParams, }; diff --git a/client/packages/react/src/index.ts b/client/packages/react/src/index.ts index 85776f4aa..f8fa17e71 100644 --- a/client/packages/react/src/index.ts +++ b/client/packages/react/src/index.ts @@ -40,6 +40,8 @@ import { type InstantSchemaDef, type BackwardsCompatibleSchema, type InstantRules, + type UpdateParams, + type LinkParams, } from "@instantdb/core"; import InstantReactAbstractDatabase from "./InstantReactAbstractDatabase"; @@ -95,4 +97,6 @@ export { type InstantSchemaDef, type BackwardsCompatibleSchema, type InstantRules, + type UpdateParams, + type LinkParams, }; diff --git a/client/sandbox/strong-init-vite/src/typescript_test_abstract.tsx b/client/sandbox/strong-init-vite/src/typescript_test_abstract.tsx index 61061156c..78012c087 100644 --- a/client/sandbox/strong-init-vite/src/typescript_test_abstract.tsx +++ b/client/sandbox/strong-init-vite/src/typescript_test_abstract.tsx @@ -1,4 +1,4 @@ -import { i, id, init, InstaQLEntity } from "@instantdb/admin"; +import { i, id, init, InstaQLEntity, UpdateParams } from "@instantdb/admin"; const _schema = i.schema({ entities: { users: i.entity({ email: i.string() }) } }); type _AppSchema = typeof _schema; @@ -13,11 +13,11 @@ const db = init({ type Collection = keyof typeof schema.entities; -type Entity = InstaQLEntity; +type EntityUpdate = UpdateParams; export const newEntity = async ( type: T, - props: Omit, "id">, + props: EntityUpdate, ) => { const theId = id(); await db.transact(db.tx[type][theId].update(props)); @@ -25,10 +25,15 @@ export const newEntity = async ( }; // seems good -const alice = await newEntity("users", { email: "alice@gmail.com" }); +const existing_attr_works = await newEntity("users", { email: "alice@gmail.com" }); -// Should be a typing error but is not -const bob = await newEntity("users", { blabla: "bob@gmail.com" }); +// @ts-expect-error +const non_existing_attr_errors = await newEntity("users", { blabla: "bob@gmail.com" }); -// Should be a typing error but is not -const eve = await newEntity("users", { email: 123 }); +// @ts-expect-error +const wrong_type_errors = await newEntity("users", { email: 123 }); + +// to silence ts warnings +existing_attr_works; +non_existing_attr_errors; +wrong_type_errors;