Argument of type partial is not assignable to parameter of type

Viewed 10692

I am trying to update partial property of one of interface in TypeScript as below

interface details{
  name: string,
  phonenumber: number,
  IsActive: boolean
}

let pDt: Partial<details> = {IsActive: false};

and when i am trying to pass it one of the method which has details as parameter, it is giving below issue "Argument of type Partial is not assignable to parameter of type 'detail'".

What's wrong here and how to correct?

3 Answers

You can, in your case if you want to use Partial, cast it like this:

interface Details{
  name: string,
  phonenumber: number,
  IsActive: boolean
}

let partialDetails: Partial<Details> = {IsActive: false};
let details: Details = partialDetails as Details;

Argument of type Partial is not assignable to parameter of type 'detail'

You can't assign a Partial of a type to the original type you made the partial from.

interface Details{
  name: string,
  phonenumber: number,
  IsActive: boolean
}

let partialDetails: Partial<Details> = {IsActive: false};
let details: Details = partialDetails // error

Playground

This makes sense because Details expects a value on all properties, but Partial<Details> can omit any of those properties. This makes the partial incompatible with the while type you made a partial of.

There's no easy "fixing" this. It depends on your app's logic. If you have a function that expects a whole Details then you have to pass it a whole Details, a Partial<Details> will not do.

If you intend to fill in extra fields on details later which is a very common JavaScript pattern just do this:

let pDt = {IsActive: false} as details;

Typescript is great when it's helping but it can often readily get in the way as well if you go overboard.

Related