mattpocock / xstate-codegen

A codegen tool for 100% TS type-safety in XState
MIT License
245 stars 12 forks source link

Example in README fails to compile with: Type 'string' does not satisfy the constraint 'never'.ts(2344) #71

Closed universalhandle closed 3 years ago

universalhandle commented 3 years ago

I installed the library globally. (If this is how the library is intended to be used, the README could be clearer about that, and I would be happy to contribute the change.)

I ran xstate-codegen "src/**/**.machine.ts" and received this output:

Scanning File:  src/machines/Demo/demo.machine.ts
Watching for file changes in: src/**/**.machine.ts

I pasted the code from the README. For posterity, should the README change, the code is:

import { Machine } from '@xstate/compiled';

interface Context {}

type Event = { type: 'DUMMY_TYPE' };

const machine = Machine<Context, Event, 'uniqueId'>({});

VS Code underlines "uniqueId." Hovering over it, the following message is displayed:

Type 'string' does not satisfy the constraint 'never'.ts(2344)

Drilling down into type definitions, I see that the third type variable is: Id extends keyof RegisteredMachinesMap<TContext, TEvent>. Am I missing a step? Am I supposed to register the machine before I can create it?

danielkcz commented 3 years ago

Theoretically, it should work globally, but I would advise you to try it locally in project deps first.

Otherwise, it feels like your VSCode hasn't caught the change in generated types. It happens sometimes, unfortunately. Opening the generated file and closing it again usually helps. Just to be sure, you can run tsc --noEmit on your project to see if there are actual TS errors.

jbeast commented 3 years ago

I was getting this error after using the example in the README (using IntelliJ IDEA rather than VSCode). For reasons I don't understand, exporting the machine is what fixed it for me 🤷‍♂️

export default machine;

DevanB commented 3 years ago

I, too, am having issues with this while setting up a new machine.

Nothing too crazy code wise:

export const assessmentMachine = createMachine<any, any, "assessmentMachine">(
danielkcz commented 3 years ago

This is more likely an environmental issue since I am doing pretty much the same it works just fine. So perhaps try to create a minimal runnable reproduction showing the problem.

DevanB commented 3 years ago

I am able to provide this error now, though I'm unsure if it will help. It seems to me that an invoke in my state machine with a type field is causing the issue.

Watching for file changes in: ./**.machine.ts
/Users/[redacted]/Projects/[redacted]/node_modules/xstate/lib/StateNode.js:223
                return invokeUtils_1.toInvokeDefinition(__assign(__assign({ id: invokeSource.type }, invokeConfig), { src: invokeSource }));
                                                                                             ^

TypeError: Cannot read property 'type' of undefined
    at /Users/[redacted]/Projects/[redacted]/node_modules/xstate/lib/StateNode.js:223:94
    at Array.map (<anonymous>)
    at new StateNode (/Users/[redacted]/Projects/[redacted]/node_modules/xstate/lib/StateNode.js:204:59)
    at /Users/[redacted]/Projects/[redacted]/node_modules/xstate/lib/StateNode.js:153:33
    at Object.mapValues (/Users/[redacted]/Projects/[redacted]/node_modules/xstate/lib/utils.js:155:23)
    at new StateNode (/Users/[redacted]/Projects/[redacted]/node_modules/xstate/lib/StateNode.js:151:23)
    at Object.createMachine (/Users/[redacted]/Projects/[redacted]/node_modules/xstate/lib/Machine.js:17:12)
    at Object.<anonymous> (/Users/[redacted]/Projects/[redacted]/assessment-machine-ts-tlxydz7kuo.xstate.js:115:36)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.module_1.default._extensions..xstate.js (/Users/[redacted]/Projects/[redacted]/node_modules/xstate-codegen/bin/extractMachines.js:72:12)
error Command failed with exit code 1.
danielkcz commented 3 years ago

@DevanB That's definitely unrelated, apparently you have an error in your code and invokeSource is undefined. It has nothing to do with the generator. The xstate-codegen will fail to generate if you have errors in your machine and this is the result.

universalhandle commented 3 years ago

Apologies for the long delay in my response. A few things came up that prevented me from getting back to this.

It looks like @FredyC was right that VS Code wasn't picking up the generated types. Thanks.