When working in Swift, I usually handle class properties of another class as implicitly unwrapped optionals like this:
class MyClass {
var prop: OtherClass!
init(prop: OtherClass) {
self.prop = prop
}
explodeProp() {
// no need to unwrap it because it's implicitly unwrapped and always assigned in the initializer
prop.explode()
}
}
However, when transitioning to TypeScript, it seems like I have to approach it differently:
export class MarkdownNode {
tag = "";
text = "";
document: FoldingDocument | null = null; // most likely never really null
parent: MarkdownNode | null = null; // might actually be null
constructor(tag: string, text: string, document: FoldingDocument) {
this.tag = tag;
this.text = text;
this.document = document;
}
addSibling(node: MarkdownNode) {
if (!this.parent) {
this.document!.nodes.push(node) // perhaps force-unwrapping is inevitable here?
} else {
this.parent.children.push(node);
}
}
}
I wonder if there exists a concept similar to implicitly unwrapped optionals in TypeScript or if it's something that might be introduced in the future?