close

[Solved] Argument type ‘unknown’ is not assignable parameter of type

I am trying to get a username But I am facing the following error: Argument type ‘unknown’ is not assignable parameter of type in TS. In this Exerror article, We are going to learn about How to reproduce this error and we will discuss All Possible Solutions Lets Get Start with This Article.

How Argument type ‘unknown’ is not assignable parameter of type Error Occurs?

I am trying to get a username But I am facing the following error:

Argument type 'unknown' is not assignable parameter of type

Here is My code.

function getUserName(username: string) {
  return username;
}

const username: unknown = 'coder';

getUserName(username);

So here I am writing all the possible solutions that I have tried to resolve this error.

How To Solve Argument type ‘unknown’ is not assignable parameter of type Error?

  1. How To Solve Argument type ‘unknown’ is not assignable parameter of type Error?

    To Solve Argument type ‘unknown’ is not assignable parameter of type Error This Error Occurs Because when you are passing value to the function and the Function is expecting a different type then this error Will Occurs So Just use type assertion while You are calling Your function Just like this: your_value as your_value_type. Using type assertion your error will be solved. Thank you.

  2. Argument type ‘unknown’ is not assignable parameter of type

    To Solve Argument type ‘unknown’ is not assignable parameter of type Error This Error Occurs Because when you are passing value to the function and the Function is expecting a different type then this error Will Occurs So Just use type assertion while You are calling Your function Just like this: your_value as your_value_type. Using type assertion your error will be solved. Thank you.

Solution 1: Use type assertion

This Error Occurs Because when you are passing value to the function and the Function is expecting a different type then this error Will Occurs So Just use type assertion while You are calling Your function Just like this: your_value as your_value_type.

Here is the Correct Code.

function getUserName(username: string) {
  return username;
}

const username: unknown = 'coder';

getUserName(username as string); // Use type Here

Using type assertion your error will be solved. Thank you.

Solution 2: You can Also Use type as any

If you don’t know value type then you can use type as any. Here is my code.

function getUserName(username: string) {
  return username;
}

const username: unknown = 'coder';

getUserName(username as any); // Use type as any Here

Conclusion

It’s all About this error. I hope We Have solved Your error. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also, Read

Leave a Comment