Issue
Hello guys I have a custom generic type P
that is defined like this P extends Record<string, unknown> | void
And I want to have an exists
function
export class Parameters<P extends Record<string, unknown> | void> {
private params: P
constructor(params: P) {
this.params = params
}
public exists(field: keyof P): boolean {
return field in this.params
}
}
But I obviously got a compilation error when running a tsc
because I can't do it on the void
type. How can I get rid of it inside my exists
function?
tsc
Typescript version 4.9.3
Thanks!
Solution
First make sure params in not undefined
export class Parameters<P extends Record<string, unknown> | void> {
params!: P
public exists(field: P): boolean {
return this.params && field in this.params
}
}
Answered By - Dimava
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.