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

Skip "uncallable" callback arguments when building union signatures (fix for #42558) #42559

Conversation

Nathan-Fenner
Copy link
Contributor

Adds a fix for the following case, where the orDefault function can't be used with empty arrays without adding type annotations:

function orDefault<T, D>(x: T | null, d: D): T | D {
    if (x === null) {
        return d;
    }
    return x;
}

const xs: string[] | null = ["a", "bc", "def"];

// y: string[] | never[]
const y = orDefault(xs, []);

// ERR: TypeScript 4.1
//  Each member of the union type 
//    (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[])
//  | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
// has signatures, but none of those signatures are compatible with each other. (2349)

// ERR: TypeScript 4.2 (dev)
// Parameter 'item' implicitly has an 'any' type.(7006)
const yThen = y.map(item => item.length);
const yChain = orDefault(xs, []).map(item => item.length);

See #42558 for details/background.

@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jan 30, 2021
@sandersn
Copy link
Member

@Nathan-Fenner It seems like #42620 addresses the bug as well, and more comprehensively. Does you agree, @weswigham?

If that's the case, probably we should close this PR.

@Nathan-Fenner
Copy link
Contributor Author

This test case from this PR might be helpful or interesting to #42620:

const f2: ((ask: (x: string, y?: number) => void) => void) | ((ask: (x: number, y: never) => void) => void) = null as any;
f2((a, b) => {
  a; // Ideally, should be string
  b; // Ideally, should be number|undefined
})

I think #42620 addresses other aspects, but I'm not 100% it handles this (rather uncommon) edge-case. Chucking it in could be useful to track its behavior in the future, anyway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants