sam-goodwin / punchcard

Type-safe AWS infrastructure.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

shape-json can't serialize a Type shape

ryan-mars opened this issue · comments

Given a file:

// main.ts
import {
  array,
  integer,
  map,
  Minimum,
  MinLength,
  number,
  string,
  Type,
} from "@punchcard/shape";
import { Json } from "@punchcard/shape-json";

class MyType extends Type({
  key: string.apply(MinLength(1)), // apply constraints with Traits
  count: integer.apply(Minimum(0)),
  rating: number,
  array: array(string),
  map: map(string),
}) {}

const mapper = Json.mapper(MyType);

const instance = new MyType({
  key: "must be a string",
  count: 1,
  rating: 1.1,
  array: ["array", "of", "strings"],
  map: {
    key: "map of string value",
  },
});

const json = mapper.write(instance);

console.log(json);

Compile and run produces an error:

.../try-shapes/node_modules/@punchcard/shape-json/lib/json.js:158
                write: (arr) => arr.map(i => item.write(i)),
                                    ^

TypeError: Cannot read property 'map' of undefined
    at Object.write (.../try-shapes/node_modules/@punchcard/shape-json/lib/json.js:158:37)
    at Object.write (.../try-shapes/node_modules/@punchcard/shape-json/lib/json.js:197:43)
    at Object.<anonymous> (.../try-shapes/out/main.js:32:19)
    at Module._compile (node:internal/modules/cjs/loader:1102:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
    at Module.load (node:internal/modules/cjs/loader:967:32)
    at Function.Module._load (node:internal/modules/cjs/loader:807:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47

tsconfig was the offender

👇🏻 works

{
  "compilerOptions": {
    "target": "es2018",
    "lib": [
      "es2019",
      "es5",
      "es6",
      "esnext.asynciterable",
      "dom"
    ],
    "downlevelIteration": true,
    "module": "commonjs",
    "strict": true,
    "strictPropertyInitialization": false,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "inlineSourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "resolveJsonModule": true,
    "declaration": true,
    "declarationMap": true
  },
  "exclude": [
    "node_modules",
    "build"
  ]
}
commented

To clarify - the target ES version was ES5 and punchcard is ES2018.