TS2345: Argument of type 'unknown' is not assignable
I have build error while using tsc:
Error:
node_modules/stream-chat/src/client.ts:1339:56 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'APIError'.

bug exist in migration from 6.7.3 to 6.8.0
"stream-chat": "6.8.0",
"stream-chat-react": "9.5.0",
"useUnknownInCatchVariables": false
only that tsconfig.ts option fixing problem, but that is not right solution....
@rgbutov thanks for reaching out, we were wondering why you were compiling this code on your side? Further, we will work on making this a bit more robust, so please follow this issue.
@rgbutov thanks for reaching out, we were wondering why you were compiling this code on your side? Further, we will work on making this a bit more robust, so please follow this issue.
I'm not compiling this code on my side:) I'm using vite/craco+react-scripts project:
tsconfig:
{
"compilerOptions": {
"baseUrl": "src",
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
stream-chat:
"stream-chat": "^8.1.0",
"stream-chat-react": "^10.3.1",
"typescript": "^4.8.4",
tsc:

@rgbutov thanks for the additional information. We've added this issue to our backlog.
It seems like your tsc ignores your "exclude": ["node_modules"] in tsconfig.
I have tried to reproduce this issue here with no luck. (I am testing with node v18.0.0) Can you please tweak the repo I have created and reproduce your error there?
getting the same error when trying to build a next application. Even tho the node_modules is in the "excluded" props. This happens when we import a type inside the application itself
We have the same issue we are unable to upgrade from version 6.7.3. The issue, for us has been introduced with 6.8.0. We also tried version 8+.
These suggestions "useUnknownInCatchVariables": false or excluding "exclude": ["node_modules"] make no difference
Workaround with 'useUnknownInCatchVariables' works for me but you know it's not good idea.
I see there is opened PR thanks to @pzmudzinski, any chance to fix the issue in near future?
@kruligh would you please have steps to reproduce? The SDK itself is type-checked so we would need to know, how to reproduce the issue. Thank you
@pzmudzinski I see your thumbs down. However, it is not clear to me, why tsc is going through the node_modules directory. I think that is the first question to be answered before creating a PR and that is why I am asking about more information about the projects setup.
I expect that those who experienced the problem of tsc compiling their node_modules files are importing from stream-chat/src. When you import directly from library/src, you are bypassing the typical entry points that would usually point to precompiled code (like main or module fields in package.json). This causes TypeScript to treat these as part of your project's source code, triggering a compilation.
@MartinCupela I do not understand why you don't want tsc to correctly compile your projects - even your internal scripts are using it (like "types": "tsc --emitDeclarationOnly true").
@pzmudzinski the root cause is incorrect import. You should not import from src, but from dist.
I do not know, why tsc should compile node_modules. I would not call that approach "correct".
I am not talking about importing library but that basic npm i fails with those errors after cloning this project:
➜ stream-chat-js git:(master) ✗ npm i
> [email protected] prepare
> yarn run build
yarn run v1.22.15
$ rm -rf dist && yarn run types && yarn run compile
$ tsc --emitDeclarationOnly true
src/client.ts:1440:56 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'APIError'.
1440 if (this.options.enableWSFallback && isWSFailure(err) && isOnline()) {
~~~
src/connection.ts:120:11 - error TS18046: 'error' is of type 'unknown'.
120 if (error.code === chatCodes.TOKEN_EXPIRED && !this.client.tokenManager.isStatic()) {
~~~~~
src/connection.ts:123:19 - error TS18046: 'error' is of type 'unknown'.
123 } else if (!error.isWSFailure) {
~~~~~
src/connection.ts:127:19 - error TS18046: 'error' is of type 'unknown'.
127 code: error.code,
~~~~~
src/connection.ts:128:25 - error TS18046: 'error' is of type 'unknown'.
128 StatusCode: error.StatusCode,
~~~~~
src/connection.ts:129:22 - error TS18046: 'error' is of type 'unknown'.
129 message: error.message,
~~~~~
src/connection.ts:130:26 - error TS18046: 'error' is of type 'unknown'.
130 isWSFailure: error.isWSFailure,
~~~~~
src/connection.ts:155:25 - error TS18046: 'error' is of type 'unknown'.
155 code: error.code,
~~~~~
src/connection.ts:156:31 - error TS18046: 'error' is of type 'unknown'.
156 StatusCode: error.StatusCode,
~~~~~
src/connection.ts:157:28 - error TS18046: 'error' is of type 'unknown'.
157 message: error.message,
~~~~~
src/connection.ts:158:32 - error TS18046: 'error' is of type 'unknown'.
158 isWSFailure: error.isWSFailure,
~~~~~
src/connection.ts:303:42 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'UR | undefined'.
303 this._log(`_connect() - Error - `, err);
~~~
src/connection.ts:372:11 - error TS18046: 'error' is of type 'unknown'.
372 if (error.code === chatCodes.TOKEN_EXPIRED && !this.client.tokenManager.isStatic()) {
~~~~~
src/connection.ts:379:11 - error TS18046: 'error' is of type 'unknown'.
379 if (error.isWSFailure) {
~~~~~
src/connection_fallback.ts:87:37 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'APIError'.
Type 'unknown' is not assignable to type 'Error'.
87 if (retry && isErrorRetryable(err)) {
~~~
src/connection_fallback.ts:118:33 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'APIError'.
118 if (isConnectionIDError(err)) {
~~~
src/connection_fallback.ts:125:24 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'Error'.
125 if (isAPIError(err) && !isErrorRetryable(err)) {
~~~
Found 17 errors in 3 files.
Errors Files
1 src/client.ts:1440
13 src/connection.ts:120
3 src/connection_fallback.ts:87
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
npm error code 2
npm error path /Users/piotr/Projects/stream-chat-js
npm error command failed
npm error command sh -c yarn run build
npm error A complete log of this run can be found in: /Users/piotr/.npm/_logs/2024-09-02T13_42_25_533Z-debug-0.log```
@pzmudzinski why would you use npm i if the project uses yarn?
@MartinCupela here you go, with yarn (no idea why it would make difference):
➜ stream-chat-js git:(master) ✗ yarn
yarn install v1.22.15
(node:49067) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] 🔍 Resolving packages...
warning @babel/cli > [email protected]: Glob versions prior to v9 are no longer supported
warning @babel/[email protected]: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-class-properties instead.
warning @babel/[email protected]: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-object-rest-spread instead.
warning @rollup/plugin-commonjs > [email protected]: Glob versions prior to v9 are no longer supported
warning @rollup/plugin-commonjs > magic-string > [email protected]: Please use @jridgewell/sourcemap-codec instead
warning eslint > file-entry-cache > flat-cache > rimraf > [email protected]: Glob versions prior to v9 are no longer supported
warning eslint-plugin-typescript-sort-keys > @typescript-eslint/experimental-utils > @typescript-eslint/typescript-estree > [email protected]: Glob versions prior to v9 are no longer supported
warning mocha > [email protected]: Glob versions prior to v9 are no longer supported
warning nyc > [email protected]: Glob versions prior to v9 are no longer supported
warning nyc > test-exclude > [email protected]: Glob versions prior to v9 are no longer supported
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
[4/4] 🔨 Building fresh packages...
success Saved lockfile.
warning Your current version of Yarn is out of date. The latest version is "1.22.22", while you're on "1.22.15".
info To upgrade, run the following command:
$ curl --compressed -o- -L https://yarnpkg.com/install.sh | bash
$ yarn run build
yarn run v1.22.15
$ rm -rf dist && yarn run types && yarn run compile
$ tsc --emitDeclarationOnly true
src/client.ts:1440:56 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'APIError'.
1440 if (this.options.enableWSFallback && isWSFailure(err) && isOnline()) {
~~~
src/connection.ts:120:11 - error TS18046: 'error' is of type 'unknown'.
120 if (error.code === chatCodes.TOKEN_EXPIRED && !this.client.tokenManager.isStatic()) {
~~~~~
src/connection.ts:123:19 - error TS18046: 'error' is of type 'unknown'.
123 } else if (!error.isWSFailure) {
~~~~~
src/connection.ts:127:19 - error TS18046: 'error' is of type 'unknown'.
127 code: error.code,
~~~~~
src/connection.ts:128:25 - error TS18046: 'error' is of type 'unknown'.
128 StatusCode: error.StatusCode,
~~~~~
src/connection.ts:129:22 - error TS18046: 'error' is of type 'unknown'.
129 message: error.message,
~~~~~
src/connection.ts:130:26 - error TS18046: 'error' is of type 'unknown'.
130 isWSFailure: error.isWSFailure,
~~~~~
src/connection.ts:155:25 - error TS18046: 'error' is of type 'unknown'.
155 code: error.code,
~~~~~
src/connection.ts:156:31 - error TS18046: 'error' is of type 'unknown'.
156 StatusCode: error.StatusCode,
~~~~~
src/connection.ts:157:28 - error TS18046: 'error' is of type 'unknown'.
157 message: error.message,
~~~~~
src/connection.ts:158:32 - error TS18046: 'error' is of type 'unknown'.
158 isWSFailure: error.isWSFailure,
~~~~~
src/connection.ts:303:42 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'UR | undefined'.
303 this._log(`_connect() - Error - `, err);
~~~
src/connection.ts:372:11 - error TS18046: 'error' is of type 'unknown'.
372 if (error.code === chatCodes.TOKEN_EXPIRED && !this.client.tokenManager.isStatic()) {
~~~~~
src/connection.ts:379:11 - error TS18046: 'error' is of type 'unknown'.
379 if (error.isWSFailure) {
~~~~~
src/connection_fallback.ts:87:37 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'APIError'.
Type 'unknown' is not assignable to type 'Error'.
87 if (retry && isErrorRetryable(err)) {
~~~
src/connection_fallback.ts:118:33 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'APIError'.
118 if (isConnectionIDError(err)) {
~~~
src/connection_fallback.ts:125:24 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'Error'.
125 if (isAPIError(err) && !isErrorRetryable(err)) {
~~~
Found 17 errors in 3 files.
Errors Files
1 src/client.ts:1440
13 src/connection.ts:120
3 src/connection_fallback.ts:87
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
It makes a difference as I cannot reproduce this problem and neither our CI.
Does this happen also with latest yarn v1 version on your side?
@MartinCupela what happens when you execute node_modules/typescript/bin/tsc from root directory?
@pzmudzinski clean result