Closed MorenoMdz closed 2 years ago
Copy-pasting my response from #331:
This is not the library problem, but the problem with how TypeScript guesses the type of the httpRequest
field. Since you have const task = { httpRequest: 'POST' }
, TypeScript decides that the type of the task
variable is { httpRequest: string }
, which cannot be be used in the API request because it expects { httpRequest: 'POST' | 'GET' | ... }
. We could've solved this by making the parameter accept any string
instead of keyof typeof ...
, but then we would've lost all compile time checks for the string enum values.
You can do the following to tell TypeScript compiler that the type of the task
is ITask
:
import {CloudTasksClient, protos} from '@google-cloud/tasks';
...
const task: protos.google.cloud.tasks.v2.ITask = {
appEngineHttpRequest: {
httpMethod: 'POST',
relativeUri: '/log_payload',
},
};
and then it will understand that httpRequest
is of type 'POST' | 'GET' | ...
and will happily pass it to the API.
Since our samples are in JavaScript and not in TypeScript, sometimes (in cases like this one) they cannot be easily copy-pasted to the TypeScript code. I took samples/quickstart.js
and made it compile in TypeScript by adding type information here and there: gist
I hope that helps!
As for the documentation question: unfortunately, at this moment we don't provide TypeScript samples, only JavaScript (even though the library itself is in TypeScript).
Environment details
@google-cloud/tasks
version: 3.0.0Steps to reproduce
Typescript will complain at the
taskClient.createTask(request)
But if we rename
httpMethod
tohttp_method
it passes, but I is probably ignoring this field and defaulting to POST anyway.Expected
Types should properly recognize the task data, documentation should provide more examples for those fields.