简体   繁体   中英

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!

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;
}

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM