In this example scenario, the developer made the mistake to name the field titleText
instead of title
.
interface ProductFoo {
recommendationId?: string;
title?: string; // Correct property name
imageUrl?: string;
}
const products: ProductFoo[] = [];
// Correct object
const result: ProductFoo = {
recommendationId: '1',
title: 'Some Title', // Correct property name
imageUrl: 'http://example.com/image.jpg',
};
products.push(result); // This will work fine
// Incorrect object (titleText does not exist on ProductFoo interface)
const wrongResult = {
recommendationId: '1',
titleText: 'Some Title', // TypeScript not giving error: 'Property 'titleText' does not exist on type 'ProductFoo'.'
imageUrl: 'http://example.com/image.jpg',
};
products.push(wrongResult);
But typescript does not give any error here.
Why is this a Big Bug?
in subsequent code, we have
mustHaveTitleOrDeleteForever(product.title)
// oops, deleted
I know I can just declare type of wrongResult: ProductFoo but is there way to keep the benefit of type inference ?