Skip to content

Instantly share code, notes, and snippets.

@matiasleidemer
Created March 31, 2020 18:06
Show Gist options
  • Save matiasleidemer/52ca8e2631bee0176d0cbc46b321fefb to your computer and use it in GitHub Desktop.
Save matiasleidemer/52ca8e2631bee0176d0cbc46b321fefb to your computer and use it in GitHub Desktop.
// TypeScript-specific way to ensure that other objects with the same shape
// can’t be interpreted as this type
declare const NsType: unique symbol
declare const LbfsType: unique symbol
// Ns effectively just wraps a value of type number.
class Ns {
readonly value: number;
[NsType]: void
constructor(value: number) {
this.value = value
}
}
// Similarly, Lbfs type wraps a number and a unique symbol.
class Lbfs {
readonly value: number;
[LbfsType]: void
constructor(value: number) {
this.value = value
}
}
function lbfsToNs(lbfs: Lbfs): Ns {
return new Ns(lbfs.value * 4.448222)
}
function trajectoryCorrection(momentum: Ns) {
if (momentum.value < new Ns(2).value) {
// disintegrate()
}
}
function provideMomentum() {
trajectoryCorrection(lbfsToNs(new Lbfs(1.5)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment