Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

angular - How to pass enum value in param for request with typescript?

I use ng-swagger-gen to generate classes, methods, etc... using swagger generated by my back end.

for this request:

  consoleUsercashOutUsingPOST(consoleUserTransactionDto: ConsoleUserTransactionDto): __Observable<ConsoleUserTransaction> {
    return this.consoleUsercashOutUsingPOSTResponse(consoleUserTransactionDto).pipe(
      __map(_r => _r.body as ConsoleUserTransaction)
    );
  }

swagger-gen generated this interface param:

/* tslint:disable */

export interface ConsoleUserTransactionDto {
  action?: 'COMPANY_ACTIVATION' | 'IN_APP_PURCHASE' | 'APP_PURCHASE' | 'MUSIC_PURCHASE' | 'EBOOK_PURCHASE' | 'USER_DOWNLOAD_APP';
  amount?: number;
  companyUuid?: number;
  consoleUserUuid?: string;
  isCashIn?: boolean;
  mobileMoneyServiceCode?: string;
  phoneNumber?: string;}

To call the method, i use it like this:

 // Initialise cash out transaction
    const consoleUserTransactionDto = {phoneNumber: this.cashOutPhoneNumberModel, consoleUserUuid: consoleUserId,
      companyUuid: companyUid, amount: amountToPay,
      mobileMoneyServiceCode: mobileMoney, isCashIn: false, action: CompanyAction.COMPANY_ACTIVATION};

    // Start cash out
    await this.consoleUserTransactionService.consoleUsercashOutUsingPOST(consoleUserTransactionDto).toPromise().then({ 

})

But I got the error

 Argument of type '{ phoneNumber: string; consoleUserUuid: string; companyUuid: number; amount: number; mobileMoneyServiceCode: any; i
sCashIn: boolean; action: CompanyAction; }' is not assignable to parameter of type 'ConsoleUserTransactionDto'.
  Types of property 'action' are incompatible.
    Type 'CompanyAction' is not assignable to type '"COMPANY_ACTIVATION" | "IN_APP_PURCHASE" | "APP_PURCHASE" | "MUSIC_PURCHASE" | "EBOOK_PURCHASE" | "USER_DOWNLOAD_APP"'.

188     await this.consoleUserTransactionService.consoleUsercashOutUsingPOST(consoleUserTransactionDto).toPromise().then(company => {

because i set action with a string action: CompanyAction.COMPANY_ACTIVATION instead of '"COMPANY_ACTIVATION" | "IN_APP_PURCHASE" | "APP_PURCHASE" | "MUSIC_PURCHASE" | "EBOOK_PURCHASE" | "USER_DOWNLOAD_APP"'

How could I do to pass the correct enum type ? Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create enum and assign to model as type of action property

example create file action-response.enum

export enum ActionResponseEnum{
   COMPANY_ACTIVATION= 'COMPANY_ACTIVATION',
   IN_APP_PURCHASE= 'IN_APP_PURCHASE',
     .......
}

then in the property

action?: ActionResponseEnum;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...