英文:
Return promise of one of the union type
问题
component_one_setup
返回 Promise<boolean>
,而 component_two_setup
返回 Promise<InitResult>
。两者都调用了一个接受 boolean 或 InitResult
类型的 resolve 的方法。但 TypeScript 抛出了以下错误:
Type '(value: boolean | PromiseLike<boolean>) => void' is not assignable to type '(value: ResolveParams) => void'.
英文:
I have got this code base whereby I am getting TS error. Can someone please help?
export type InitResult = {canInit: boolean; reason: string}
export type ResolveParams = InitResult | boolean
export function selectMethod(
payload: {resolve?: (value: ResolveParams) => void} = {},
): void {
//
return undefined
}
const component_one_setup = (): Promise<boolean> =>
new Promise(resolve => {
// ts error: Type '(value: boolean | PromiseLike<boolean>) => void' is not assignable to type '(value: ResolveParams) => void'.
selectMethod({resolve})
})
const component_two_setup = (): Promise<InitResult> =>
new Promise(resolve => {
// ts error: Type '(value: InitResult | PromiseLike<InitResult>) => void' is not assignable to type '(value: ResolveParams) => void'
selectMethod({resolve})
})
component_one_setup
returns Promise<boolean>
and component_two_setup
returns Promise<InitResult>
. Both are calling a method that would accept resolve of boolean or InitResult
.
But Typescript is throwing error:
Type '(value: boolean | PromiseLike<boolean>) => void' is not assignable to type '(value: ResolveParams) => void'
答案1
得分: 1
Convert the selectMethod
function to a TS generic function.
export type InitResult = { canInit: boolean; reason: string };
export function selectMethod<Resolve extends (value: any) => void>(payload: { resolve?: Resolve }): void {
payload.resolve?.(true)
// or
payload.resolve?.({ canInit: false, reason: 'a' })
return undefined
}
const component_one_setup = (): Promise<boolean> => new Promise((resolve) => {
selectMethod({ resolve })
});
const component_two_setup = (): Promise<InitResult> => new Promise((resolve) => {
selectMethod({ resolve })
});
英文:
Convert the selectMethod
function to a TS generic function.
export type InitResult = { canInit: boolean; reason: string };
export function selectMethod<Resolve extends (value: any) => void>(payload: { resolve?: Resolve }): void {
payload.resolve?.(true)
// or
payload.resolve?.({ canInit: false, reason: 'a' })
return undefined
}
const component_one_setup = (): Promise<boolean> => new Promise((resolve) => {
selectMethod({ resolve })
});
const component_two_setup = (): Promise<InitResult> => new Promise((resolve) => {
selectMethod({ resolve })
});
答案2
得分: 0
我通过修改如下代码成功解决了上述问题:
export type InitResult = { canInit: boolean; reason: string };
export type ResolveParams = InitResult | boolean;
export type ResolveFunction = (value: InitResult | boolean) => void;
export function selectMethod(payload: { resolve?: ResolveFunction } = {}): void {
//
return undefined;
}
const component_one_setup = (): Promise<boolean> => new Promise((resolve) => {
selectMethod({ resolve: resolve as ResolveFunction });
});
const component_two_setup = (): Promise<InitResult> => new Promise((resolve) => {
selectMethod({ resolve: resolve as ResolveFunction });
});
<details>
<summary>英文:</summary>
I managed to solve above issue by modifying the code as below:
export type InitResult = {canInit:boolean; reason:string};
export type ResolveParams = InitResult | boolean;
export type ResolveFunction = (value:InitResult | boolean)=>void;
export function selectMethod(payload: { resolve?: ResolveFunction} = {}): void {
//
return undefined
}
const component_one_setup = (): Promise<boolean> => new Promise((resolve) => {
selectMethod({ resolve: resolve as ResolveFunction })
});
const component_two_setup = (): Promise<InitResult> => new Promise((resolve) => {
selectMethod({ resolve: resolve as ResolveFunction })
});
[working example][1]
[1]: https://www.typescriptlang.org/play?ssl=16&ssc=4&pln=1&pc=1#code/KYDwDg9gTgLgBDAnmYcCSA7AljASsAZwFcAbeAXjjgG8BjAQw0xwC4AjCCE4Rgbjig8CEDCwIwoWDAHMAvrwBQoSLATJU%20YSQBuwAAr0o9ALYE4lZnkKl4AHzgcuPDIuXR4SFHE1ddAMSIMWhgsEXM4AAptehIiYBZLTRs4e0duRgBKcgA%20bQgsABNFJXB3OAAzQODQjDgCYG5ggFlgGAALCAKIsHpEEgh6ApYaAUJfYAB%20YZ8dYACgkJFZcOpZDOG8wpoFOAB6XZ3BGCIoWsCC4HKpYAKFWQUFWhFxOCfjSAxgDBgAfRFgH71Y5gcIRdZwPRQCDGLD1AA8aWc2XMyM%20AHcIVCYfUIhFBFpdBkUds6g1gM1Wh0utRRgT4rTxnB6GYZv4qotams7hlik8MC83h8vr8YGiIIDWkQQZQwcNIdDYcA4YlrGRkTk4OjMQqcXixrMiRrqDtSY0YC12p0IjT8eNhrbZkyWfq2QsanAuWteEA
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论