Issue
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 'PageableLaravel'
export interface PageableLaravel {
path: string;
current_page: number;
from: number;
}
export interface Pageable {
success: boolean; // <-"Property 'success' of type 'boolean' is not assignable to string index type 'PageableLaravel'
message: string; // "Property 'message' of type 'string' is not assignable to string index type 'PageableLaravel'
[key: string]: PageableLaravel
}
The [key: string]
will always change, for example:
Below is an example of a JSON returned from an API
{
success: true,
message: 'success',
pages: {//<-key name always will change
path: "XX"
//etc
}
}
{
success: true,
message: 'success',
products: {//<-key name always will change
path: "XX"
//etc
}
}
{
success: true,
message: 'success',
brands: {//<-key name always will change
path: "XX"
//etc
}
}
Is there a way to declare a key that may/may not always change?
Solution
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
}
Answered By - Tim Perry
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.