-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved typing for union types (#2151)
* Have unions not devolve into `IAnyType` with 10+ members. * Improve typings for enums * Remove no longer used scripts/generate-union-types.js * Add an additional literal to union typechecking test * spike: issue 1525 still broken * test: add test for issue 1664 * spike: failing test for 1833 * test: remove failing tests * test: add describe/test blocks * test: remove failing tests (again) * test: add test for issue 1525 --------- Co-authored-by: Tyler Williams <[email protected]>
- Loading branch information
1 parent
46334b6
commit 8238701
Showing
6 changed files
with
141 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { types as t } from "../../src/index" | ||
|
||
describe("1664. Array and model types are not inferred correctly when broken down into their components", () => { | ||
test("should not throw a typescript error", () => { | ||
// Simple concrete type with a creation type different than its instance type | ||
const date = t.custom<string, Date>({ | ||
name: "Date", | ||
fromSnapshot: (snapshot) => new Date(snapshot), | ||
toSnapshot: (dt) => dt.toISOString(), | ||
isTargetType: (val: unknown) => val instanceof Date, | ||
getValidationMessage: (snapshot: unknown) => | ||
typeof snapshot !== "string" || isNaN(Date.parse(snapshot)) | ||
? `${snapshot} is not a valid Date string` | ||
: "" | ||
}) | ||
|
||
//Wrap the date type in an array type. IArrayType is a sub-interface of IType. | ||
const DateArray = t.array(date) | ||
|
||
//Pass the array type to t.union, which infers the component types as <C, S, T> | ||
const LoadableDateArray = t.union(t.literal("loading"), DateArray) | ||
|
||
//Instantiate the type | ||
const lda = LoadableDateArray.create([]) | ||
|
||
//Try to use the array type as an instance | ||
if (lda !== "loading") { | ||
//Error: type of lda is essentially `(string | Date)[] | undefined` | ||
//The creation type has been mixed together with the instance type | ||
const dateArray: Date[] = lda | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.