Grigory Bogush
Grigory Bogush

Reputation: 413

Typescript. How to specify a type of a subset of object literal properties?

Specify a type as in 'has property of this type' but may have other properties that we don't care about.

I have the two interfaces:

interface IForm {
 form: {
   name: string;
   email: string;
   picture: File | null;
 }
}

and a sub interface

interface IUpload {
 form {
   picture: File | null;
 }
}

How would I specify in the IUpload interface that the form variable should not be that exactly, but rather just have this field, so that the extends relationship would work in this case. IForm extends IUpload?

Sorry if I didn't formulate the question correctly. Thanks in advance!

Upvotes: 0

Views: 273

Answers (2)

DaggeJ
DaggeJ

Reputation: 2201

You're good as it is, I just completed your code and corrected a few typos :) IForm can extend IUpload as it has the picture property.

interface IForm extends IUpload {
    form: {
        name: string;
        email: string;
        picture: File | null;
    }
}

export interface IUpload {
    form:
    {
        picture: File | null;
    }
}

Upvotes: 1

rrd
rrd

Reputation: 5977

I think this might work for you:

interface IForm {
  name: string;
  email: string;
  picture: File | null;
}

Then in your IUpload:

interface IUpload extends Partial<IForm> {
  other: things;
}

Upvotes: 0

Related Questions