Skip to content

Argument of type 'object & Record<"property", unknown>' is not assignable to parameter of type 'Interface'. #61619

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
DonaldDuck313 opened this issue Apr 26, 2025 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@DonaldDuck313
Copy link

DonaldDuck313 commented Apr 26, 2025

🔎 Search Terms

Argument of type 'object & Record<"property", unknown>' is not assignable to parameter of type 'Interface'.
Types of property 'property' are incompatible.
Type 'unknown' is not assignable to type 'number'.(2345)

🕗 Version & Regression Information

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

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgJLmvJyDeAoZQ5ABygHtjowBPALmRAFcBbAI2gG48BfPPAEwgIANnCgoYjEAjDAyIZDAAUxMXGb10kWIggBKegDcywflwFDR45AnkBnMMjj0pAaxBkA7iC7BlNSjJlOD1kAF4I5AAiMlYAKyEwKOQAMhSnZABCSKZhYVT0qNIKKmpk0Ay05ACIIKU4ADpiyigaUIiw6KY2aCi9fCJFer0uXiA

💻 Code

interface Interface {
    property: number;
}

declare function f(param: Interface): void;

declare const a: unknown;
if(typeof(a) === "object" && a !== null && "property" in a && typeof(a.property) === "number"){
    f(a);
}

🙁 Actual behavior

I the following TS2345 error:

Argument of type 'object & Record<"property", unknown>' is not assignable to parameter of type 'Interface'.
  Types of property 'property' are incompatible.
    Type 'unknown' is not assignable to type 'number'.

🙂 Expected behavior

I shouldn't get any error. Inside the if block, nothing is of type unknown:

  • a itself is a non-null object because of typeof(a) === "object" && a !== null, so a itself is not of type unknown
  • a.property is a number because of "property" in a && typeof(a.property) === "number", so a.property is not of type unknown

So it makes no sense to get any error in the if block related to the type unknown.

While a may have other properties that are unknown, they're not referenced anywhere so that shouldn't matter.

Additional information about the issue

The following code which from a typing perspective does exactly the same thing works just fine:

interface Interface {
    property: number;
}

declare function f(param: Interface): void;

declare const a: unknown;
if(typeof(a) === "object" && a !== null && "property" in a && typeof(a.property) === "number"){
    const b: object = a;
    const c: number = a.property;
}

I don't see why this code should compile but not the other code.

Workarounds are to give a the type any instead of unknown or to use f(a as Interface), but both those workarounds defeat the purpose of what I'm trying to do since they would compile even if I made a mistake in the if condition.

@MartinJohns
Copy link
Contributor

Duplicate of #42384.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 28, 2025
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 1, 2025
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