Unexpected error because Human-friendly type descriptions are not possible for circular types that are not objects or unions
amagee opened this issue · 1 comments
amagee commented
Small example:
src/mytypes.ts
:
import createValidator, { createDetailedValidator, registerType } from "typecheck.macro";
export type ProjectLayerConfig = {
layers: Array<Group | Layer>;
};
registerType("ProjectLayerConfig");
type Item = {
id: string;
label: string;
isVisible: boolean;
};
registerType("Item");
export type Group = Item & {
type: "Group";
children: Array<Group | Layer>;
};
registerType("Group");
export type Layer = Item & {
type: "Layer";
children: SubLayer[];
};
registerType("Layer");
export type SubLayer = Item & {
type: "SubLayer"
};
registerType("SubLayer");
const validator = createDetailedValidator<ProjectLayerConfig>();
package.json
:
{
"name": "typescript-babel",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"compile": "babel src --out-dir dist --extensions \".ts\""
},
"author": "",
"license": "ISC",
"dependencies": {
"@babel/cli": "^7.12.16",
"@babel/core": "^7.12.16",
"@babel/plugin-transform-modules-commonjs": "^7.12.13",
"@babel/preset-typescript": "^7.12.16",
"babel-plugin-macros": "^3.0.1",
"typecheck.macro": "^0.7.1"
}
}
Output:
> typescript-babel@1.0.0 compile /path/typescript-babel
> babel src --out-dir dist --extensions ".ts"
MacroError: /path/typescript-babel/src/mcve.ts: Unexpected error because Human-friendly type descriptions are not possible for circular types that are not objects or unions. Please contact me, I didn't know this was possible!. Please report this to the developer.
at throwUnexpectedError (/path/typescript-babel/node_modules/typecheck.macro/dist/macro-assertions.js:91:9)
at visitInstantiatedType (/path/typescript-babel/node_modules/typecheck.macro/dist/code-gen/irToHumanFriendlyDescription.js:116:49)
at visitIR (/path/typescript-babel/node_modules/typecheck.macro/dist/code-gen/irToHumanFriendlyDescription.js:73:10)
at /path/typescript-babel/node_modules/typecheck.macro/dist/code-gen/irToHumanFriendlyDescription.js:185:21
at Array.map (<anonymous>)
at visitObjectPattern (/path/typescript-babel/node_modules/typecheck.macro/dist/code-gen/irToHumanFriendlyDescription.js:183:22)
at visitIR (/path/typescript-babel/node_modules/typecheck.macro/dist/code-gen/irToHumanFriendlyDescription.js:73:10)
at humanFriendlyDescription (/path/typescript-babel/node_modules/typecheck.macro/dist/code-gen/irToHumanFriendlyDescription.js:24:10)
at /path/typescript-babel/node_modules/typecheck.macro/dist/code-gen/irToInline.js:868:177
at wrapFalsyExprWithErrorReporter (/path/typescript-babel/node_modules/typecheck.macro/dist/code-gen/irToInline.js:869:5) {
name: 'MacroError',
code: 'BABEL_TRANSFORM_ERROR'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! typescript-babel@1.0.0 compile: `babel src --out-dir dist --extensions ".ts"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the typescript-babel@1.0.0 compile script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/amagee/.npm/_logs/2021-02-15T22_28_06_776Z-debug.log
Note: if I use createValidator
instead of createDetailedValidator
, it succeeds.
vedantroy commented
Hi! Thanks for the bug report. Will look into it.