typecheck.macro icon indicating copy to clipboard operation
typecheck.macro copied to clipboard

Syntax error when typing lambda parameters inside constraints

Open ducaale opened this issue 3 years ago • 1 comments

In a newly created CRA project, the following code

type NumberContainer = {
  pos: Positive;
}
type Positive = number;

registerType('NumberContainer')

export const x = createValidator<NumberContainer>(undefined, {
  constraints: {
    Positive: (x: Positive) => x > 0
  }
})

Fails with

SyntaxError: /Users/ducaale/lesson-editor/src/lessonValidation.ts: typecheck.macro: /Users/ducaale/lesson-editor/src/lessonValidation.ts: Unexpected token, expected "," (1:3)

> 1 | ((x: Positive) => x > 0)
    |    ^ Learn more: https://www.npmjs.com/package/typecheck.macro
This error occurred during the build time and cannot be dismissed.

Since I have strict mode enabled in my tsconfig.json, I can't leave x untyped and typing it would lead to syntax error from typecheck.macro. My workaround at the moment is to disable ts checking with ts-ignore

export const x = createValidator<NumberContainer>(undefined, {
  constraints: {
    // @ts-ignore
    Positive: x => x > 0
  }
})

ducaale avatar Apr 23 '21 16:04 ducaale

This seems to be a problem that only happens in create-react-app since I don't get any syntax errors once I eject my app.

Edit: I have opened https://github.com/facebook/create-react-app/issues/10906 regarding this issue.

ducaale avatar Apr 30 '21 18:04 ducaale