gajus / flow-runtime

A runtime type system for JavaScript with full Flow compatibility.
MIT License
802 stars 57 forks source link

flow-runtime ParameterType remember doesn't work with arrays #237

Open vlsergeyatsbt opened 5 years ago

vlsergeyatsbt commented 5 years ago

This is a:

Which concerns:


What is the current behaviour?

class TestClass<ValueType>  {

  do(  ) {
    const items : ValueType[] = JSON.parse( '[{ "a": null, "b": "b" }, { "a": "a", "b": null }]' );
    console.log(items);
  }

}

const testClass : TestClass = new TestClass();
testClass.do();
RuntimeTypeError: [1].a must be null
Expected: null
Actual Value: "a"
Actual Type: string
-------------------------------------------------
[1].b must be a string
Expected: string
Actual Value: null
Actual Type: null

What is the expected behaviour?

Shall not throw an exception.


Which package versions are you using?

    "@babel/cli": "^7.5.5",
    "@babel/core": "^7.5.5",
    "@babel/plugin-proposal-class-properties": "^7.5.5",
    "@babel/plugin-proposal-decorators": "^7.4.4",
    "@babel/preset-env": "^7.5.5",
    "@babel/preset-flow": "^7.0.0",
    "babel-plugin-flow-runtime": "^0.19.0",
    "flow-runtime": "^0.17.0"
{
  "plugins": [
    [ "flow-runtime", {
      "assert": true,
      "annotate": true
      } ],
    [ "@babel/plugin-proposal-decorators", { "decoratorsBeforeExport": true}],
    "@babel/plugin-proposal-class-properties",
  ],
  "presets": [
    "@babel/preset-flow",
    "@babel/preset-env",
  ]
}
vlsergey commented 5 years ago

For anyone who have the same issue. Workaround is to remove type template and use "internal" type alias:

type ValueType = any;

class TestClass  {

  do(  ) {
    const items : ValueType[] = JSON.parse( '[{ "a": null, "b": "b" }, { "a": "a", "b": null }]' );
    console.log(items);
  }

}

const testClass : TestClass = new TestClass();
testClass.do();