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

Incorrect Generic compatibility report #29062

Closed
zheeeng opened this issue Dec 17, 2018 · 3 comments
Closed

Incorrect Generic compatibility report #29062

zheeeng opened this issue Dec 17, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@zheeeng
Copy link

zheeeng commented Dec 17, 2018

TypeScript Version: 3.2.2

Search Terms:

Code
It is a common situation that provides some default params to required options and make them be optional to bring convenient calling usage.

interface HasHeight { 
  height: number
}

type Omit<P, K extends keyof P> = Pick<P, Exclude<keyof P, K>>

function makeHeightOptional<P extends HasHeight>(heightRequired: (p: P) => number) {
  return function heightOptional(params: Omit<P, 'height'> & { height?: number }) {
    const height = params.height || 10
    const newParams = { ...params, height }

    // Argument of type 'Pick<P, Exclude<keyof P, "height">> & { height: number; }' is not assignable to parameter of type 'P'.
    return heightRequired(newParams)
  }
}

Expected behavior:

code works

Actual behavior:

TS report:

Argument of type 'Pick<P, Exclude<keyof P, "height">> & { height: number; }' is not assignable to parameter of type 'P'.

Playground Link: http://www.typescriptlang.org/play/index.html#src=interface%20HasHeight%20%7B%20%0A%20%20height%3A%20number%0A%7D%0A%0Atype%20Omit%3CP%2C%20K%20extends%20keyof%20P%3E%20%3D%20Pick%3CP%2C%20Exclude%3Ckeyof%20P%2C%20K%3E%3E%0A%0Afunction%20makeHeightOptional%3CP%20extends%20HasHeight%3E(heightRequired%3A%20(p%3A%20P)%20%3D%3E%20number)%20%7B%0A%20%20return%20function%20heightOptional(params%3A%20Omit%3CP%2C%20'height'%3E%20%26%20%7B%20height%3F%3A%20number%20%7D)%20%7B%0A%20%20%20%20const%20height%20%3D%20params.height%20%7C%7C%2010%0A%20%20%20%20const%20newParams%20%3D%20%7B%20...params%2C%20height%20%7D%0A%0A%20%20%20%20return%20heightRequired(newParams)%0A%20%20%7D%0A%7D%0A%0A

Related Issues:

@jack-williams
Copy link
Collaborator

Think this is a duplicate of #28884

@ahejlsberg
Copy link
Member

Duplicate of #28884.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants