Skip to content

Commit 43fab81

Browse files
authored
Approach Resource typing differently in the UI (#58)
Previously the Resource typing was awkward at best - asserting Resources to concrete types seamed like needles work that defied the point of having them defined as interfaces One potential solution was asserting string literal to string literal, but that felt backwards, example: "literal" as "literal" Someone in the Github thread[0] suggested asserting them in the following manner: "literal" as const - which is a bit better, but still misses the point Due to technical reasons the literal inferred type was string, rather than the literal itself. What seams to solve the problem is asserting the whole object to be a Resource. That also solves the problem with creeping import scope while at the same time setting up correct guards at compile time Last, but not least I think that the documentation on type assertion is misleading at best (couldn't find an example describing the mechanism and didn't feel like going through the source) - the mechanism is much more safe than described in the official docs, which cover the case of any -> T. Whereas in the more general case of T -> U assertion the overlap is checked (so that if I make a typo in the object an error is raised) [0] microsoft/TypeScript#25889
1 parent e87b676 commit 43fab81

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

src/App.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export default class App extends Component<{
2727
{
2828
super(props);
2929

30-
// TODO: get rid of the casting
31-
const ourTree = makeTree({__typename: "Text", content: "korzen", is_done: false} as Text);
30+
const initial_resource: Resource = {__typename: "Text", content: "korzen", is_done: false};
31+
const ourTree = makeTree(initial_resource);
3232

3333
this.state= {
3434
chosenNode: ourTree.root.id,
@@ -206,11 +206,10 @@ function displayTree(t: Tree<Resource>, chosenNode: NodeID): ReactTreeGraphNode
206206
}
207207

208208

209-
// TODO: get rid of the casting
210209
function displayResource(r: Resource): string
211210
{
212211
return {
213-
"Link": (r as unknown as Link).address,
212+
"Link": (r as Link).address,
214213
"Text": (r as Text).content,
215214
}[r.__typename];
216215
}

src/ResourceAdder.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// TODO: how to limit importing scope?
2-
import { Resource, ResourceTypeString, ResourceTypeStringValues, Link, Text } from "./core/resources";
1+
import { Resource, ResourceTypeString, ResourceTypeStringValues } from "./core/resources";
32

43
import React, { FunctionComponent } from "react";
54
import { Formik, Form, Field } from "formik";
@@ -40,11 +39,10 @@ const ResourceAdder: React.FunctionComponent<{
4039
export default ResourceAdder;
4140

4241

43-
// TODO: get rid of the casting
4442
function default_for_typestring(t: ResourceTypeString): Resource
4543
{
4644
return {
47-
"Link": { __typename: "Link", address: "", is_done: false } as Link,
48-
"Text": { __typename: "Text", content: "", is_done: false } as Text,
45+
"Link": { __typename: "Link", address: "", is_done: false } as Resource,
46+
"Text": { __typename: "Text", content: "", is_done: false } as Resource,
4947
}[t];
5048
}

src/ResourceEditor.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const ResourceEditor: React.FunctionComponent<{
2727
__typename: props.resource.__typename,
2828
};
2929

30-
props.onEditonCommit(resource as any);
30+
props.onEditonCommit(resource as Resource);
3131
}}
3232
>
3333
{

0 commit comments

Comments
 (0)