Skip to content

Instantly share code, notes, and snippets.

@rescribet
Last active April 19, 2018 12:06
Show Gist options
  • Save rescribet/bdf043881ef0dff14cc080b51bef21ef to your computer and use it in GitHub Desktop.
Save rescribet/bdf043881ef0dff14cc080b51bef21ef to your computer and use it in GitHub Desktop.
Variable types don't seem to be filtered by comparing them via an array includes statement.
abstract class SomeNode {
public readonly termType!: string;
}
class NamedNode extends SomeNode {
public readonly termType: "NamedNode";
constructor() {
super();
this.termType = "NamedNode";
}
}
class Literal extends SomeNode {
public readonly termType: "Literal";
constructor() {
super();
this.termType = "Literal";
}
}
class BlankNode extends SomeNode {
public readonly termType: "BlankNode";
constructor() {
super();
this.termType = "BlankNode";
}
}
class Collection extends SomeNode {
public readonly termType: "Collection";
constructor() {
super();
this.termType = "Collection";
}
}
type SomeTerm = NamedNode | BlankNode | Literal | Collection | undefined;
function subset(x: NamedNode | BlankNode): void {
console.log(x);
}
function example(x: SomeTerm): void {
// if (!x || x.termType === "Literal" || x.termType === "Collection") does work however
if (!x || ["Literal", "Collection"].includes(x.termType)) {
return;
}
subset(x); // This should be valid
}
example(new NamedNode());
example(new BlankNode());
example(new Collection());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment