Xstate: Compilation issues with Typescript & xstate 4.0.0-13 & 4.0.0-14

Created on 16 Oct 2018  路  25Comments  路  Source: davidkpiano/xstate

Hi David,

Since version 4.0.0-13, I'm having the following compilation issues:

node_modules/xstate/lib/actionTypes.d.ts(1,30): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(1,30): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(2,29): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(2,29): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(3,30): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(3,30): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(4,29): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(4,29): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(5,31): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(5,31): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(6,30): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(6,30): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(8,31): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(8,31): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(9,30): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(9,30): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(10,34): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(10,34): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(11,28): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(11,28): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(12,29): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(12,29): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.
node_modules/xstate/lib/actionTypes.d.ts(13,31): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(13,31): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal.

Why isn't there any import of ActionTypes in actionTypes.d.ts?

No idea for the TS1254 error.

I wasn't able to reproduce in codesandox.io, there the compilation seems to work fine. If you need it, I can provide you with a sample project reproducing the issue.

help wanted typescript

Most helpful comment

I've checked out xstate and build & tests are running fine but I noticed the actionTypes.d.ts declaration file is missing the enum import import { ActionTypes } from './types'; when using TypeScript 3.1.3. Import is still there in 3.0.3 so it looks like a regression at TypeScript side.

Someone has just created a similar bug for that: https://github.com/Microsoft/TypeScript/issues/27962

May I suggest that in your build pipeline, you fetch the latest version of xstate and run some automated tests on in? This will allow you to detect if something is wrong when using your library which is currently not catched by your build.

All 25 comments

Maybe it is related to the TypeScript version you're using. What TS version are you using?

I am using TypeScript 2.9.2.

Here's my package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "test",
  "main": "main.js",
  "scripts": {
    "compile": "rm -rf dist && tsc -p . --diagnostics"
  },
  "repository": {
    "type": "git",
    "url": ""
  },
  "author": "",
  "license": "ISC",
  "engines": {
    "node": ">=8.11.2"
  },
  "devDependencies": {
    "@types/node": "^10.5.3",
    "typescript": "2.9.2"
  },
  "dependencies": {
    "xstate": "4.0.0-14"
  }
}

and tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "jsx": "preserve",
    "esModuleInterop": true,
    "sourceMap": true,
    "allowJs": true,
    "lib": [
      "es6",
      "dom"
    ],
    "rootDir": "src",
    "moduleResolution": "node"
  }
}

I am running NodeJS v10.10.0.

Try with TypeScript v3.

Also happy to accept a PR if you figure out what's going on.

I tried with Typescript 3.1.3 & xstate 4.0.0-15 but I get the following errors:

node_modules/xstate/es/types.d.ts(269,18): error TS2430: Interface 'AtomicStateNodeConfig<TContext, TEvent>' incorrectly extends interface 'StateNodeConfig<TContext, never, TEvent>'.
  Types of property 'initial' are incompatible.
    Type 'undefined' is not assignable to type 'never'.

=> can be fixed by changinginitial?: undefined; to initial?: never;

node_modules/xstate/lib/types.d.ts(269,18): error TS2430: Interface 'AtomicStateNodeConfig<TContext, TEvent>' incorrectly extends interface 'StateNodeConfig<TContext, never, TEvent>'.
  Types of property 'initial' are incompatible.
    Type 'undefined' is not assignable to type 'never'.

=> can be fixed by changinginitial?: undefined; to initial?: never;

node_modules/xstate/lib/actionTypes.d.ts(1,30): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(1,30): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(2,29): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(2,29): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(3,30): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(3,30): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(4,29): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(4,29): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(5,31): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(5,31): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(6,30): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(6,30): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(8,31): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(8,31): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(9,30): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(9,30): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(10,34): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(10,34): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(11,28): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(11,28): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(12,29): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(12,29): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(13,31): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(13,31): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.

=> can be fixed by adding import {ActionTypes} from 'xstate';

Any idea why I'm getting such compilations errors and you don't?

I'm not sure, but can you PR your fix and I'll try it locally, and pull it in if it still works?

I've checked out xstate and build & tests are running fine but I noticed the actionTypes.d.ts declaration file is missing the enum import import { ActionTypes } from './types'; when using TypeScript 3.1.3. Import is still there in 3.0.3 so it looks like a regression at TypeScript side.

Someone has just created a similar bug for that: https://github.com/Microsoft/TypeScript/issues/27962

May I suggest that in your build pipeline, you fetch the latest version of xstate and run some automated tests on in? This will allow you to detect if something is wrong when using your library which is currently not catched by your build.

Using import { ActionTypes } from './types'; in actionTypes.d.ts doesn't remove this error for me (xstate v4):

node_modules/xstate/lib/actionTypes.d.ts(1,30): error TS2304: Cannot find name 'ActionTypes'.
node_modules/xstate/lib/actionTypes.d.ts(1,30): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
node_modules/xstate/lib/actionTypes.d.ts(2,29): error TS2304: Cannot f ...

Should we revert to TypeScript 3.0.x?

If that fixes the issue I'd recommend it for the short-term. At the moment this prevents me using xstate v4 in a typescript environment. Sorry I can't provide a better solution for this.

Yes, please revert to version 3.0.3 of Typescript which didn't include that regression, we are also stuck with version 4.0.0-12 since that issue.

Published as [email protected]

node_modules/xstate/es/types.d.ts(269,18): error TS2430: Interface 'AtomicStateNodeConfig<TContext, TEvent>' incorrectly extends interface 'StateNodeConfig<TContext, never, TEvent>'.
  Types of property 'initial' are incompatible.
    Type 'undefined' is not assignable to type 'never'.

From comparing xstate's tsconfig.json and mine I discovered that this error does not pop up when it has this property (in compilerOptions):

"strictNullChecks": true

... no clue why, but I'll use it 馃槂

I recommend "strictNullChecks": true anyway 馃槈

I'm trying, but now I have 20+ compilation issues 馃槶

Still, have this issue in 4.1.2

@bodia-uz Which errors are you getting specifically? And what version of TS do you have?

error:

node_modules/xstate/lib/types.d.ts:288:18 - error TS2430: Interface 'AtomicStateNodeConfig<TContext, TEvent>' incorrectly extends interface 'StateNodeConfig<TContext, never, TEvent>'.
  Types of property 'initial' are incompatible.
    Type 'undefined' is not assignable to type 'never'.

288 export interface AtomicStateNodeConfig<TContext, TEvent extends EventObject> extends StateNodeConfig<TContext, never, TEvent> {
                     ~~~~~~~~~~~~~~~~~~~~~

node_modules/xstate/lib/types.d.ts:288:18 leeds to next line:
https://github.com/davidkpiano/xstate/blob/v4.1.2/src/types.ts#L414

xstate: 4.1.2
typescript: 3.1.6
compillerOptions:

{
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": false,
    "noUnusedLocals": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es5",
    "jsx": "react",
    "lib": ["dom", "es2016"]
}

If you replace:

-  extends StateNodeConfig<TContext, never, TEvent> {
+  extends StateNodeConfig<TContext, StateSchema, TEvent> {

does your error go away?

Yes. With this replacement, the error goes away.

@davidkpiano thanks for quick reaction!

Hi @davidkpiano,

When do you plan on releasing 4.1.3 including this fix?

KR,

Laurent

Probably later today.

That's a long day ;-)

@pvdyck 4.2.0 just released!

@pvdyck 4.2.0 just released!

thanks ! great ! wonderful !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amelon picture amelon  路  3Comments

carloslfu picture carloslfu  路  3Comments

bradwoods picture bradwoods  路  3Comments

drmikecrowe picture drmikecrowe  路  3Comments

hnordt picture hnordt  路  3Comments