Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function Overload To Union #20909

Open
venusliang opened this issue Dec 20, 2022 · 2 comments · May be fixed by #20910
Open

Function Overload To Union #20909

venusliang opened this issue Dec 20, 2022 · 2 comments · May be fixed by #20910
Labels
new-challenge Propose a new challenge, a PR will be auto generated

Comments

@venusliang
Copy link

Please follow the template and fill the info. A PR will be auto-generated and always reflect on your changes.

Detailed solution/guide is not required, but please be sure the challenge is solvable.

Info

Basic info of your challenge questions,

difficulty: extreme
title: Function Overload To Union
tags: union, overload, function

Question

Implement a type OverloadToUnion that transforms all overloads of a function into a union.

Template

type OverloadToUnion<F> = any;

Test Cases

Provide some test cases for your challenge, you can use some utils from @type-challenges/utils for asserting.

import type { Equal, Expect } from '@type-challenges/utils'
import { ExpectFalse, NotEqual } from '@type-challenges/utils'

type Tab = {
    id: number;
    url?: string;
}
declare function queryTab(callback: (user: Tab) => void): void;
declare function queryTab(id: number): Tab;
declare function queryTab(name: string): Promise<Tab>;


type Details = {
    code?: string
}
type InsertCSSFunc = {
    (details: Details): Promise<void>;
    (details: Details, callback?: () => void): void;
    (tabId: number, details: Details): Promise<void>;
    (tabId: number, details: Details, callback?: () => void): void;
}

type cases = [
    Expect<Equal<OverloadToUnion<typeof queryTab>, ((callback: (user: Tab) => void) => void) | ((id: number) => Tab) | ((name: string) => Promise<Tab>)>>,
    Expect<Equal<OverloadToUnion<InsertCSSFunc>,
        ((details: Details) => Promise<void>) |
        ((details: Details, callback?: (() => void) | undefined) => void) |
        ((tabId: number, details: Details) => Promise<void>) |
        ((tabId: number, details: Details, callback?: (() => void) | undefined) => void)>
    >,
]
@venusliang venusliang added the new-challenge Propose a new challenge, a PR will be auto generated label Dec 20, 2022
@github-actions github-actions bot linked a pull request Dec 20, 2022 that will close this issue
@github-actions
Copy link
Contributor

#20910 - Pull Request updated.

2022-12-20T11:08:55.737Z Preview in Playground

@venusliang
Copy link
Author

Possible solution: Playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-challenge Propose a new challenge, a PR will be auto generated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant