javascript - Does flow's structural subtyping "forget" specific subtype properties? -


i studying structural typing. skeptical idea 2 types considered equivalent because happen have portion of structure in common. feels lot static duck typing , ignores semantic level of types. took closer @ flow's structural typing of ordinary objects , have encountered following behavior:

const o:{} = {foo: true}; o.foo; // type error 

{} structural type , supertype of ordinary objects. hence makes sense can annotate o it, because {foo: true} structural subtype of {}. however, when try access existing foo property, operation doesn't type check. odd because afaik structural subtype can contain specific properties long includes required properties of supertype.

it seems flow's structural subtyping algorithm forgets properties specific subtype. behavior intended or did run edge case?

the overall issue you're describing fact of casting subtype supertype. performing cast, you're explicitly telling compiler discard information given object.

for instance, without structural typing, if do

class animal {}  class cat extends animal {   foo: bool = true; }  const c: animal = new cat();  console.log(c.foo); 

(on flow.org/try)

it fails typecheck same reason. in example you've explicitly told compiler "consider o have type {}", in example i've said "consider c have type animal". because of that, compiler has explicitly been told forget working cat, therefore forgets object has .foo property.

it seems flow's structural subtyping algorithm forgets properties specific subtype. behavior intended or did run edge case?

so answer this, doesn't "occasionally", when tell it. behavior absolutely intended.

this odd because afaik structural subtype can contain specific properties long includes required properties of supertype.

the object contain property, 100% fine, you've explicitly erased information casting value supertype.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -