Microsoft/TypeScript

Argument of type X is not assignable to parameter of type Y, but then assignable. - Misleading error

Open

#50 438 ouverte le 25 août 2022

Voir sur GitHub
 (3 commentaires) (0 réactions) (0 assignés)TypeScript (6 726 forks)batch import
Cursed?Experience EnhancementHelp WantedSuggestion

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

Description

Bug Report

🔎 Search Terms

Argument of type is not assignable to parameter of type, object properties overlap

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries.

The closest I could find in the FAQ is this https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-all-types-assignable-to-empty-interfaces

Type { color: "red", margin: 1 } has the required member of Props, so it may correctly be evaluated by TS as fitting, however, the error in the first place is then misleading.

⏯ Playground Link

same result with 4.8.0-beta

Playground link with relevant code

💻 Code

export const is = <TargetType,>() => <T extends TargetType>(arg: T): T => arg

type Props = { color: string }

let x = is<Props>()({ margin: 1 }) // ERROR: Argument of type '{ margin: number; }' is not assignable to parameter of type 'Props'. Object literal may only specify known properties, and 'margin' does not exist in type 'Props'.(2345)
let y = is<Props>()({ color: "red", margin: 1 }) // margin .... OK

Since {margin: 1} is clearly tolerated, the error given is misleading as the actual error is not in the presence of margin but in the absence of color, which is a property required by the Props type.

🙁 Actual behavior

Argument of type '{ margin: number; }' is not assignable to parameter of type 'Props'. Object literal may only specify known properties, and 'margin' does not exist in type 'Props'.(2345)

🙂 Expected behavior

Argument of type '{ margin: number; }' is not assignable to parameter of type 'Props'. Object literal does not specify properties required by 'Props'.(2345)

Guide contributeur