Skip to content

Inconsistent type conversion for *js.Object #682

Closed
@flimzy

Description

@flimzy

I've discovered that doing a type switch on a *js.Object behaves differently than an explicit type assert. Playground link

package main

import (
	"fmt"

	"github.com/gopherjs/gopherjs/js"
)

func main() {
	obj := js.Global.Get("Object").New()
	obj.Set("foo","bar")
	
	fmt.Printf("obj.foo = %s\n", obj.Get("foo").String())
	
	var x interface{} = obj
	switch t := x.(type) {
	case *js.Object:
	    fmt.Printf("t.foo = %s\n", t.Get("foo").String())
	}
	
	if y, ok := x.(*js.Object); ok {
	    fmt.Printf("y.foo = %s\n", y.Get("foo").String())
	}
}

Produces the following output:

obj.foo = bar 
t.foo = undefined 
y.foo = bar 

where I would have expected:

obj.foo = bar 
t.foo = bar
y.foo = bar 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions