Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected error because Human-friendly type descriptions are not possible for circular types that are not objects or unions #17

Open
amagee opened this issue Feb 15, 2021 · 1 comment

Comments

@amagee
Copy link

amagee commented Feb 15, 2021

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:

> [email protected] 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! [email protected] compile: `babel src --out-dir dist --extensions ".ts"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] 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
Copy link
Owner

Hi! Thanks for the bug report. Will look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants