From 9be6e0f386547c7b5a58eedd3566bbfa6a63fece Mon Sep 17 00:00:00 2001 From: Javier Gonzalez Date: Fri, 7 May 2021 23:36:17 +0200 Subject: [PATCH] generic types are now inferable from constructor params --- CHANGELOG.md | 4 +++ packages/lib/package.json | 2 +- packages/lib/src/modelShared/prop.ts | 11 +++--- packages/lib/test/model/defaultProps.test.ts | 5 +++ packages/lib/test/model/subclassing.test.ts | 35 +++++++++++--------- 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2876c5e..0dfbdac6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 0.58.2 + +- Generic types are now inferable from the constructor parameters + ## 0.58.1 - Fixed an issue where `idProp` on the base was not being picked up by `ExtendedModel`. diff --git a/packages/lib/package.json b/packages/lib/package.json index 9d6f36c0..54af4437 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -1,6 +1,6 @@ { "name": "mobx-keystone", - "version": "0.58.1", + "version": "0.58.2", "description": "A MobX powered state management solution based on data trees with first class support for Typescript, snapshots, patches and much more", "keywords": [ "mobx", diff --git a/packages/lib/src/modelShared/prop.ts b/packages/lib/src/modelShared/prop.ts index bc679db2..2db3bd76 100644 --- a/packages/lib/src/modelShared/prop.ts +++ b/packages/lib/src/modelShared/prop.ts @@ -52,12 +52,11 @@ export type ModelPropsToData = { } // we don't use O.Optional anymore since it generates unions too heavy -export type ModelPropsToCreationData = O.Pick< - { - [k in keyof MP]?: MP[k]["$creationValueType"] - }, - OptionalModelProps -> & +// also if we use pick over the optional props we will loose the ability +// to infer generics +export type ModelPropsToCreationData = { + [k in keyof MP]?: MP[k]["$creationValueType"] +} & O.Omit< { [k in keyof MP]: MP[k]["$creationValueType"] diff --git a/packages/lib/test/model/defaultProps.test.ts b/packages/lib/test/model/defaultProps.test.ts index fa6f5cf1..6af39ed0 100644 --- a/packages/lib/test/model/defaultProps.test.ts +++ b/packages/lib/test/model/defaultProps.test.ts @@ -51,6 +51,11 @@ test("default props", () => { bb?: number bbbb?: number | null + + a?: number + aaa?: number | null + b?: number + bbb?: number | null } & { a: number aaa: number | null diff --git a/packages/lib/test/model/subclassing.test.ts b/packages/lib/test/model/subclassing.test.ts index 4c96eb45..47597ff2 100644 --- a/packages/lib/test/model/subclassing.test.ts +++ b/packages/lib/test/model/subclassing.test.ts @@ -86,11 +86,12 @@ test("subclassing with additional props", () => { x?: number | null y?: number | null z?: number | null - } & Empty & { - a?: number | null - } & { - b: number - } + } & { + a?: number | null + b?: number + } & { + b: number + } & Empty ) const p2 = new P2({ x: 20, b: 70 }) @@ -300,11 +301,11 @@ test("three level subclassing", () => { x?: number | null | undefined y?: number | null | undefined z?: number | null | undefined - } & Empty & { - a?: number | null | undefined - } & { - b: number - } + } & { + a?: number | null | undefined + } & { b?: number } & { + b: number + } & Empty ) const p2 = new P2({ x: 20, b: 70 }) @@ -697,21 +698,22 @@ test("ExtendedModel should bring static / prototype properties", () => { test("new pattern for generics", () => { @model("GenericModel") class GenericModel extends Model(() => ({ - v1: prop(), + v1: prop(), v2: prop(), v3: prop(0), })) {} assert( _ as ModelData>, - _ as { [modelIdKey]: string; v1: string; v2: number; v3: number } + _ as { [modelIdKey]: string; v1: string | undefined; v2: number; v3: number } ) assert( _ as ModelData>, - _ as { [modelIdKey]: string; v1: number; v2: string; v3: number } + _ as { [modelIdKey]: string; v1: number | undefined; v2: string; v3: number } ) - const s = new GenericModel({ v1: "1", v2: 2, v3: 3 }) + const s = new GenericModel({ v1: "1", v2: 2, v3: 3 }) + assert(s, _ as GenericModel) expect(s.v1).toBe("1") expect(s.v2).toBe(2) expect(s.v3).toBe(3) @@ -720,11 +722,12 @@ test("new pattern for generics", () => { class ExtendedGenericModel extends ExtendedModel(() => ({ baseModel: modelClass>(GenericModel), props: { - v4: prop(), + v4: prop(), }, })) {} - const e = new ExtendedGenericModel({ v1: "1", v2: 2, v3: 3, v4: 4 }) + const e = new ExtendedGenericModel({ v1: "1", v2: 2, v3: 3, v4: 4 }) + assert(e, _ as ExtendedGenericModel) expect(e.v1).toBe("1") expect(e.v2).toBe(2) expect(e.v3).toBe(3)