Skip to content

Open Can't use type aliases resolving to Promise for async/await return types #32574

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

Closed
zaoqi opened this issue Jul 26, 2019 · 2 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@zaoqi
Copy link

zaoqi commented Jul 26, 2019

TypeScript Version: 3.5.3

Search Terms:

Code

type CPS<x> = Promise<x> // to use async/await
function callcc<T>(f: (k: (v: T) => CPS<any>) => CPS<T>): CPS<T> {
    return new Promise((resolve, reject) => {
        const resolve_packed: (v: T) => CPS<any> = (v) => {
            resolve(v)
            return new Promise((resolve, reject) => { })
        }
        f(resolve_packed).then(resolve).catch(reject)
    })
}
function new_cps<T>(x: T): CPS<T> {
    return Promise.resolve(x)
}
function un_cps<T>(x: CPS<T>): T {
    let r: [true, T] | [false, any] | null = null
    x.then((v) => { r = [true, v]; }).catch((e) => { r = [false, e]; })
    if (r === null) {
        throw 'ERR'
    } else if (r[0] === true) {
        return r[1]
    } else if (r[0] === false) {
        throw String(r[1])
    }
    throw 'ERR'
}
async function f(): CPS<any> {
    // ...
}

Expected behavior:

Actual behavior:

a.ts:26:21 - error TS1055: Type 'CPS' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.

26 async function f(): CPS<any> {
                       ~~~~~~~~


Found 1 error.

Playground Link:

Related Issues:

#27987

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 31, 2019
@RyanCavanaugh
Copy link
Member

The error means what it says. If you want to do this, you also need a value named CPS in scope that refers to a valid Promise constructor.

const CPS = Promise;
// Proceed as expected, everything OK:

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants