TypeScript interface [key: string]

前端 未结 2 1755
别跟我提以往
别跟我提以往 2021-01-24 06:35

I\'m trying to write an interface but it keeps popin\' an error:

\"Property \'success\' of type \'boolean\' is not assignable to string index type \'PageableLarave

2条回答
  •  猫巷女王i
    2021-01-24 07:16

    Unfortunately that isn't allowed with the index signature syntax that you're using. When you add the index signature ([key: string]: PageableLaravel) that means that all properties must be PageableLaravel, not just all unspecified properties.

    You can solve this with an intersection type though:

    export interface PageableLaravel {
        path: string;
        current_page: number;
        from: number;   
    }
    
    export type Pageable = {
        success: boolean;
        message: string;
    } & {
        [key: string]: PageableLaravel
    }
    

    TypeScript playground demo

提交回复
热议问题