Skip to content

Incorrect Generic compatibility report #29062

@zheeeng

Description

@zheeeng

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions