-
Notifications
You must be signed in to change notification settings - Fork 57
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
error TS2345: Argument of type 'ISchoolService'
is not assignable to parameter of type 'ServiceDefinition<UntypedServiceImplementation>'
#144
Comments
I got a similar error when I was building mine. I was able to resolve the issue by adding the following line to the
import { Server, ServerCredentials } from '@grpc/grpc-js'
import { GreeterServiceService } from './proto/greet_grpc_pb'
import { GreeterService } from './service'
const main = () => {
const server = new Server()
server.bindAsync(
'0.0.0.0:50051',
ServerCredentials.createInsecure(),
(err: Error | null, port: number) => {
if (err) console.error('Error caught: ' + err)
server.addService(GreeterServiceService, new GreeterService())
console.log(`gRPC Server listening on port ${port}`)
},
)
}
main()
import { handleUnaryCall, UntypedHandleCall } from '@grpc/grpc-js'
import { IGreeterServiceServer } from './proto/greet_grpc_pb'
import { GreetRequest, GreetResponse } from './proto/greet_pb'
export class GreeterService implements IGreeterServiceServer {
[name: string]: UntypedHandleCall \\ <---THE FIX IS HERE
greet: handleUnaryCall<GreetRequest, GreetResponse> = (call, callback) => {
const request = call.request
const response = new GreetResponse()
response.setResult(
'Hello, ' + request.getName() + '! Welcome to gRPC World :)',
)
callback(null, response)
}
} I hope this helps! |
Doesn't help. |
Would you mind sharing the code? |
I can't find the file where the error occurred. Could you link it? |
So I poked around and I think the compilation of your decorators might be part of the problem as an example this is my compiled file for my greeter service.
// package: greeter
// file: greet.proto
/* tslint:disable */
/* eslint-disable */
import * as grpc from "@grpc/grpc-js";
import * as greet_pb from "./greet_pb";
interface IGreeterServiceService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
greet: IGreeterServiceService_IGreet;
}
interface IGreeterServiceService_IGreet extends grpc.MethodDefinition<greet_pb.GreetRequest, greet_pb.GreetResponse> {
path: "/greeter.GreeterService/Greet";
requestStream: false;
responseStream: false;
requestSerialize: grpc.serialize<greet_pb.GreetRequest>;
requestDeserialize: grpc.deserialize<greet_pb.GreetRequest>;
responseSerialize: grpc.serialize<greet_pb.GreetResponse>;
responseDeserialize: grpc.deserialize<greet_pb.GreetResponse>;
}
export const GreeterServiceService: IGreeterServiceService;
// HERE
export interface IGreeterServiceServer extends grpc.UntypedServiceImplementation {
greet: grpc.handleUnaryCall<greet_pb.GreetRequest, greet_pb.GreetResponse>;
}
export interface IGreeterServiceClient {
greet(request: greet_pb.GreetRequest, callback: (error: grpc.ServiceError | null, response: greet_pb.GreetResponse) => void): grpc.ClientUnaryCall;
greet(request: greet_pb.GreetRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: greet_pb.GreetResponse) => void): grpc.ClientUnaryCall;
greet(request: greet_pb.GreetRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: greet_pb.GreetResponse) => void): grpc.ClientUnaryCall;
}
export class GreeterServiceClient extends grpc.Client implements IGreeterServiceClient {
constructor(address: string, credentials: grpc.ChannelCredentials, options?: Partial<grpc.ClientOptions>);
public greet(request: greet_pb.GreetRequest, callback: (error: grpc.ServiceError | null, response: greet_pb.GreetResponse) => void): grpc.ClientUnaryCall;
public greet(request: greet_pb.GreetRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: greet_pb.GreetResponse) => void): grpc.ClientUnaryCall;
public greet(request: greet_pb.GreetRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: greet_pb.GreetResponse) => void): grpc.ClientUnaryCall;
} The interface that you have declare in the file is missing the inheritance of This is the commands I used to generate my files.
npm install grpc_tools_node_protoc_ts --save-dev
# generate js codes via grpc-tools
grpc_tools_node_protoc \
--js_out=import_style=commonjs,binary:./proto \
--grpc_out=grpc_js:./proto \
--plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` \
-I ./proto \
./proto/*.proto
# generate d.ts codes
protoc \
--plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts \
--ts_out=grpc_js:./proto \
-I ./proto \
./proto/*.proto Hopefully this helps :) Just an FYI I am just getting more familiar with writing TypeScript gRPC code. |
Your build script seems to work but when I run the application:
|
you need to use a package called ts-node |
I generate grpc Typescript code with:
and hit the following error:
Environment
apt
What do I miss?
The text was updated successfully, but these errors were encountered: