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

Type 'WebStream<string>' is not assignable to type 'string'. #1546

Closed
thapecroth opened this issue Jul 22, 2022 · 6 comments
Closed

Type 'WebStream<string>' is not assignable to type 'string'. #1546

thapecroth opened this issue Jul 22, 2022 · 6 comments

Comments

@thapecroth
Copy link

Got the following error when running

tsc --noEmit

node_modules/openpgp/openpgp.d.ts:10:85 - error TS2307: Cannot find module '@openpgp/web-stream-tools' or its corresponding type declarations.

10 import type { WebStream as GenericWebStream, NodeStream as GenericNodeStream } from '@openpgp/web-stream-tools';
                                                                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/utils/utils.ts:11:3 - error TS2322: Type 'WebStream<string>' is not assignable to type 'string'.

On openpgp version 5.3.0

I believe this is cause by the fact that in the package.json, it listed as devdependency instead of dependency !?

@thapecroth
Copy link
Author

Turn out I can just fixed it by install opengpp 5.3.1.

@srn271
Copy link

srn271 commented Sep 12, 2022

I get the same error when upgrading from 5.10 to >=5.2.0 && <= 5.5.0.

The error is probably due to the changes in this commit:
d89cc48

const encrypted: string = await openpgp.encrypt({
  message, // openpgp.Message<string>
  encryptionKeys, // openpgp.PublicKey[]
  signingKeys // openpgp.PrivateKey[]
})

@srn271
Copy link

srn271 commented Sep 12, 2022

Nevermind. Installing the latest version of @openpgp/web-stream-tools (0.0.11) into devDependencies fixes the issue.

Should be mentioned in the documentation or even in the release notes, as already addressed in #1502 (comment).

@rene-leanix
Copy link

Having to install an additional package like @openpgp/web-stream-tools to fix the typing isn't a great solution, of course, but it also doesn't seem to work any more either.

The reason it doesn't work is that @openpgp/web-stream-tools does not export the interface NodeStream (any more?), which is why TypeScript treats it as any, with the result that the conditional T extends NodeStream<infer X> is always evaluated as true, so the resulting type is NodeStream<string>, even if T is string. In other words, the return type of openpgp.encrypt<string>() is Promise<NodeStream<string>> and not Promise<string>.

This can be fixed by changing

export function encrypt<T extends MaybeStream<Data>>(options: EncryptOptions & { message: Message<T>, format?: 'armored' }): Promise<
  T extends WebStream<infer X> ? WebStream<string> :
  T extends NodeStream<infer X> ? NodeStream<string> :
  string
>;

to

export function encrypt<T extends MaybeStream<Data>>(options: EncryptOptions & { message: Message<T>, format?: 'armored' }): Promise<
  T extends ReadableStream<unknown> ? (
    T extends WebStream<unknown> ? WebStream<string> :
    T extends NodeStream<unknown> ? NodeStream<string> :
    string
  ) : string
>;

As this check is done in several places, it may be good to define a new generic type of it, e.g.:

type StreamOrSimple<MessageType, SimpleReturnType> = MessageType extends ReadableStream<unknown> ? (
  MessageType extends WebStream<unknown> ? WebStream<SimpleReturnType> :
  MessageType extends NodeStream<unknown> ? NodeStream<SimpleReturnType> :
  SimpleReturnType
) : SimpleReturnType;

export function encrypt<T extends MaybeStream<Data>>(options: EncryptOptions & { message: Message<T>, format?: 'armored' }): Promise<StreamOrSimple<T, string>>;
export function encrypt<T extends MaybeStream<Data>>(options: EncryptOptions & { message: Message<T>, format: 'binary' }): Promise<StreamOrSimple<T, Uint8Array>>;
export function encrypt<T extends MaybeStream<Data>>(options: EncryptOptions & { message: Message<T>, format: 'object' }): Promise<Message<T>>;

Should I create a PR along those lines, @larabr?

@larabr
Copy link
Collaborator

larabr commented Feb 19, 2024

@rene-leanix please see this: #1720 (comment) , it should resolve TS issues with v5.

@rene-leanix
Copy link

@larabr, yes, installing @openpgp/web-stream-tools@0.0.11-patch-0 also fixes the issue, as it still contains a definition of NodeStream. Thanks! I missed that issue.

It should still be preferable to adjust the typing to avoid the need to install @openpgp/web-stream-tools.

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

4 participants