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

Improved typing for union types #2151

Merged
merged 12 commits into from
Mar 1, 2024
Prev Previous commit
test: add test for issue 1525
  • Loading branch information
coolsoftwaretyler authored and thegedge committed Mar 1, 2024
commit 7886cbbc13224ba4e945065abbdb549033b0730d
33 changes: 33 additions & 0 deletions __tests__/core/1525.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { types, Instance } from "../../src/index"

describe("1525. Model instance maybe fields becoming TypeScript optional fields when included in a types.union", () => {
it("does not throw a typescript error", () => {
const Model = types.model("myModel", {
foo: types.string,
bar: types.maybe(types.integer)
})

const Store = types.model("store", {
itemWithoutIssue: Model,
itemWithIssue: types.union(types.literal("anotherValue"), Model)
})

interface IModel extends Instance<typeof Model> {}

interface FunctionArgs {
model1: IModel
model2: IModel
}

const store = Store.create({
itemWithoutIssue: { foo: "works" },
itemWithIssue: { foo: "has ts error in a regression" }
})

const f = (props: FunctionArgs) => {}

const itemWithoutIssueModel = store.itemWithoutIssue
const itemWithIssueModel = store.itemWithIssue === "anotherValue" ? null : store.itemWithIssue
itemWithIssueModel && f({ model1: itemWithoutIssueModel, model2: itemWithIssueModel })
})
})