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

[@grpc/grpc-js] Broken ClientOptions types #1558

Closed
smolijar opened this issue Sep 1, 2020 · 0 comments
Closed

[@grpc/grpc-js] Broken ClientOptions types #1558

smolijar opened this issue Sep 1, 2020 · 0 comments

Comments

@smolijar
Copy link
Contributor

smolijar commented Sep 1, 2020

Problem description

ClientOptions type is intersection of its definition and Partial<ChannelOptions>. Partial<ChannelOptions> defines constants and index signature [key: string]: string | number | undefined;, which contradicts with ClientOptions defintions: Since intersection is used, all string attributes of ClientOptions are required to be string | number | undefined, which contradicts for example ClientOptions's interceptors, which is array of functions.

This issue makes the new interceptor unusable with TS without force-casting.

Reproduction steps

import { Interceptor, Client, ChannelCredentials } from "@grpc/grpc-js";

const interceptor = (null as any) as Interceptor; // just type demonstration
new Client("", ChannelCredentials.createInsecure(), {
  interceptors: [interceptor], // triggers error, whole options object
});
Argument of type '{ interceptors: Interceptor[]; }' is not assignable to parameter of type 'ClientOptions'.
  Type '{ interceptors: Interceptor[]; }' is not assignable to type 'Partial<ChannelOptions>'.
    Property 'interceptors' is incompatible with index signature.
      Type 'Interceptor[]' is not assignable to type 'string | number'.
        Type 'Interceptor[]' is not assignable to type 'string'.

Environment

  • @grpc/js 1.1.5
  • Tested on TS 4.0.2 and 3.9.7

Additional context

We could solve this either by removing the index signature or removing it before the type intersection, which is a bit hard on eyes with generics. Will try to post a PR soon.

smolijar added a commit to smolijar/grpc-node that referenced this issue Sep 1, 2020
Remove index signature from ChannelOptions to fix intersection error
described in grpc#1558 which causes issues on using ClientOptions direct
fields with TypeScript.

Removing of index signature required few minor changes:
 - adding few constant types that were used throughout the app
 - using `as const` assertion in xds-client
 - using not-so-great type cast in channelOptionsEqual

Alternative solution would be removing the index signature from
ChannelOptions explicitly in ClientOptions definition, which is not
trivial and probably calls for a generic type helper.

See: grpc#1558
Fixes: 1558
smolijar added a commit to smolijar/grpc-node that referenced this issue Sep 1, 2020
Remove index signature from ChannelOptions to fix intersection error
described in grpc#1558 which causes issues on using ClientOptions direct
fields with TypeScript.

Removing of index signature required few minor changes:
 - adding few constant types that were used throughout the app
 - using `as const` assertion in xds-client
 - using not-so-great type cast in channelOptionsEqual

Alternative solution would be removing the index signature from
ChannelOptions explicitly in ClientOptions definition, which is not
trivial and probably calls for a generic type helper.

See: grpc#1558
Fixes: grpc#1558
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

1 participant