You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript不同接口类型变量传入函数的类型兼容疑问

Why does TypeScript allow passing a Car type variable to a function expecting Fruit?

Great question! This boils down to TypeScript's structural type system—a core design choice that sets it apart from languages like Java or C# which use nominal type systems.

Let me break it down simply:

  • In a nominal type system, compatibility depends on the name of the type. Even if two types have identical properties, if their names are different, they’re not considered compatible.
  • In TypeScript’s structural system, compatibility is based on the actual shape/structure of the type. If one type has all the properties (with matching types) that another type requires, it’s deemed compatible—no matter what the type is named.

In your specific example:

  • The Fruit interface only requires a name: string property.
  • Your Car interface has that exact name: string property, plus an extra wheels: number property.

Since the fruitOnly function only ever accesses the name property of its parameter, a Car instance is perfectly safe to pass in—it has everything the function needs to run correctly. TypeScript recognizes this safety and doesn’t throw an error.

Here’s your code again to make this concrete:

interface Fruit { name: string }
interface Car { name: string, wheels: number }

function fruitOnly( paramFruit: Fruit ): void { 
  console.log( paramFruit.name + " is a fruit!" ); 
}

const myCar: Car = { name: "Tesla", wheels: 4 };
fruitOnly(myCar); // No error—Car has all the properties Fruit requires

This design is intentional: it makes TypeScript incredibly flexible. For example, if you’re using a third-party function that only needs an object with a name property, you can pass in any of your own types that include that property without writing extra type conversions or wrappers.

If you do want to enforce that only Fruit types (and not compatible shapes like Car) are allowed, you can add a unique "brand" property to the Fruit interface to make its structure distinct:

interface Fruit { 
  name: string;
  _isFruit: never; // Unique marker property to distinguish Fruit from other types
}
interface Car { 
  name: string; 
  wheels: number;
}

const myCar: Car = { name: "Tesla", wheels: 4 };
fruitOnly(myCar); // Now this throws an error—Car lacks the _isFruit property

内容的提问来源于stack exchange,提问作者Ben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:03:52