Skip to content

Instantly share code, notes, and snippets.

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

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 Sep 22, 2018
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.
View nested-ternary.go
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 Sep 22, 2018
Emulation of a JavaScript ternary operation in Golang. I would not recommend ever actually using this, just use an if.
View ternary.go
package main
import (
"fmt"
)
func main() () {
predicate := true
fmt.Println(map[bool]string{true: "a", false: "b"}[predicate])
}
You can’t perform that action at this time.