0

I'm using typescript and nlp.js/
But the argument manager says 'Parameter manager implicitly has an any type.
I tried to use : but it doesn't work well. How can I type in this case? Thank you for reading it.

module.exports = async function trainnlp(manager) { await manager.addAnswer('en', 'greetings.arrival', "I've missed you"); manager.save('./model.nlp', true); } 
0

2 Answers 2

5

How to type async function using typescript

The type of an async function's return value is Promise<X>, where X is the fulfillment value that will be provided. In your case, since your function doesn't have a return anywhere, it would be Promise<void>, since the promise created by your function will be fulfilled with nothing in particular. (At runtime the fulfillment value will be undefined, but for type purposes, it's modelled as Promise<void>.)

Recent versions of TypeScript will default that return type for you, which is why the error relates to manager...

But the argument manager says 'Parameter manager implicitly has an any type.

This is the real issue with that code: manager has no type. The point of TypeScript is to provide strong typing at compile-time. It can't do that for your trainnlp function if you don't tell it what type manager should be. You want to provide a type for manager:

// (Just an example type) interface Manager { addAnswer(a: string, b: string, c: string): Promise<void>; save(a: string, b: boolean): void; } async function trainnlp(manager: Manager) { // ----------------------------^^^^^^^^^ await manager.addAnswer('en', 'greetings.arrival', "I've missed you"); manager.save('./model.nlp', true); } 

On the playground

That clears the problem the error is flagging up. TypeScript will then infer the return type of the function, since it sees that it's an async function but never uses return, so its type is unambiguously Promise<void>.

Sign up to request clarification or add additional context in comments.

5 Comments

is function return nothing
@constraintAutomaton - I don't understand that...question? async functions always return a Promise.
No he wants to wait for is a promise to be completed before saving is manager, plus it's not really a question of async I'm pretty sure it's the typescript compiler. The error is Parameter manager implicitly has any type and manager is the parameter of is function.
I'm pretty sure your function returns nothing, it has no return statement. the addAnswer(a: string, b: string, c: string) return a promise but it doesn't need a type because the expression is not declared in a variable or the return statement of a function.
@constraintAutomaton - I suggest reading up on async functions. Again, async functions always return a Promise. That's how they propagate results (specifically in this case, success [fulfillment] or failure [rejection]). The type of trainnlp is Promise<void> (but as I said in the answer above, that type can be inferred by the compiler, it doesn't need to be explicitly provided).
-1

It's not an async function problem, it's a compiler problem. In your tsconfig.json you have the flag noImplicitAny as true you can turn it to false. But it's a bad idea, for such a trivial case.

A better way to do it would be to specify the type of manager you can even set it to any if you really want, but I don't think you want it.

module.exports = async function trainnlp(manager:any) { await manager.addAnswer('en', 'greetings.arrival', "I've missed you"); manager.save('./model.nlp', true); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.