type FruitName = 'apple' | 'banana';
interface Fruit {
name: FruitName;
}
const apple = {
'name': 'apple',
};
function test(fruit: Fruit) {
console.log(fruit.name);
}
function main() {
const name: FruitName = 'apple'; // this is permissible
test(apple); // Error TS2345: Argument of type '{ name: string; }' is not suitable for the parameter of type 'Fruit'. Type 'FruitName'
// Types of property 'name' are inconsistent.
// Type 'string' cannot be assigned to type 'FruitName'.
}
main();
I am puzzled by why apple cannot be assigned to Fruit.
However, 'apple' can be assigned to name: FruitName.
What is the distinction between the two?