Skip to content

Instantly share code, notes, and snippets.

@Insulince
Insulince / Meta Type Narrowing.md
Last active March 8, 2019 17:30
Type narrowing an object based on the presence (or lack thereof) of the very type narrowing function itself on that object.

Meta Type Narrowing

class Model {
  static isModel(object: any): object is Model { // `object` is an instance of `Model` class, if...
    return !!object && // It is truthy AND...
      !!object.__proto__.constructor.isModel && // It contains a static property with the same name as this function AND...
      typeof object.__proto__.constructor.isModel === "function"; // That static property is a function.
  }
}
@Insulince
Insulince / nested-ternary.go
Created September 22, 2018 01:05
An example of nesting the Golang version of a JavaScript ternary operation into itself an arbitrary amount of times. Again, and I cannot stress this enough, DO NOT DO THIS.
package main
import (
"fmt"
)
func main() () {
for num := 0; num < 16; num++ {
fmt.Println(num, map[bool]string{
true: map[bool]string{
@Insulince
Insulince / ternary.go
Created September 22, 2018 01:02
Emulation of a JavaScript ternary operation in Golang. I would not recommend ever actually using this, just use an if.
package main
import (
"fmt"
)
func main() () {
predicate := true
fmt.Println(map[bool]string{true: "a", false: "b"}[predicate])
}