-
Notifications
You must be signed in to change notification settings - Fork 522
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
Type safety for JSON serializable things #136
Comments
Typescript complains abt. the circular reference if I attempt to support nested arrays like this (e.g., [[[ . ]]]): export type JsonArray = (JsonPrimitive | JsonObject | JsonArray)[]; (Perhaps @skylerjokiel, @anthony-murphy or @jack-williams have a trick.) |
it can be worked around with more interfaces, but recursive types are coming in typescript 3.7: |
Yeah until microsoft/TypeScript#33050 lands it has to be done with an interface: export interface JsonArray extends Array<JsonPrimitive | JsonObject | JsonArray> {} |
Note: Json introduced in #165 |
The Jsonable definition works well for anonymous objects, but requires casting through 'unknown' in both directions when casting to/from a compatible interface. :-/ interface ICell {
text: JsonablePrimitive;
cache: JsonablePrimitive;
}
private loadCell(row: number, col: number): ICell {
return this.matrix.getItem(row, col) as unknown as ICell;
}
private storeCell(row: number, col: number, cell: ICell) {
return this.matrix.setItems(row, col, [cell as unknown as Jsonable]);
} |
Yeah this is abit of a pain. You can always do: type ICell = {
text: JsonablePrimitive;
cache: JsonablePrimitive;
} or interface ICell {
text: JsonablePrimitive;
cache: JsonablePrimitive;
[other: string]: unknown
} The latter is probably more honest unless you're explicitly filtering out properties before serialising. Could also do: private loadCell(row: number, col: number): ICell {
return this.matrix.getItem(row, col) as Pick<ICell, keyof ICell>;
}
private storeCell(row: number, col: number, cell: Pick<ICell, keyof ICell>) {
return this.matrix.setItems(row, col, [cell as Jsonable]);
} |
tagging myself for future developments on this issue |
We have had a lot of discussion about this over the past year, and some things were implemented, like AsJsonable. However, the general consensus has been that this requires too much "fancy type handling" and ultimately is more confusing than helpful. The current plan is to close this, and possibly remove the things we put in place already with follow up issues. |
Because this issue is marked as "won't fix" and has not had activity for over 3 days, we're automatically closing it for house-keeping purposes. |
From Tyler's Soduku feedback:
This is approximately the solution:
As I've mentioned before (#3145), we should also call Object.freeze(..) on these objects.
The text was updated successfully, but these errors were encountered: