JSMonk / hegel

An advanced static type checker
https://hegel.js.org
MIT License
2.09k stars 59 forks source link

Properties in function arguments should be invariant #74

Closed vkurchatkin closed 4 years ago

vkurchatkin commented 4 years ago

Small example:

const a: { a: number} = { a: 1 };
const b: {a : number | string } = a; // CORRECT, not allowed

function foo(obj: { a : number | string }) {}
const f: ({ a: number}) => undefined = foo; // INCORRECT, allowed
JSMonk commented 4 years ago
  1. If you will use cast between link type it will be incorrect behavior, because the initial value can be changed. https://jsmonk.github.io/hegel/docs/array-types#subtyping
  2. The. the function is ok, because of in foo function will be provided only number https://jsmonk.github.io/hegel/docs/type-compatibility#function-types-compatibility
JSMonk commented 4 years ago

Thank you for contribution ^_^

vkurchatkin commented 4 years ago

No, it's not ok. Here is a simple example of a runtime error:

function foo(obj: { a : number | string }) {
    obj.a = 'foo';
}

const f: ({ a: number}) => undefined = foo; 

const obj = { a: 1 };

f(obj);

obj.a.toFixed(); // BOOM
JSMonk commented 4 years ago

Hmmm, really. I Will fixed it soon. Thank you a lot for your contribution ^_^