Found the solution - using Record<never, never>
instead of Record<string, never>
fixes the type-checking:
type CatData = {
name: string;
breed: string;
age: number;
};
type MaybeCatData = Record<never, never> | CatData;
// Now TypeScript shows errors as expected
function processCat(obj: MaybeCatData): CatData {
return {
name: obj.name, // Error ✗
breed: obj.breed, // Error ✗
age: obj.age // Error ✗
};
}
// Safe version with type narrowing:
function processCatSafely(obj: MaybeCatData): CatData {
if ('name' in obj) {
return {
name: obj.name,
breed: obj.breed,
age: obj.age
};
}
throw new Error('Invalid cat data');
}
While Record<string, never>
allows string properties (with never
values), Record<never, never>
represents an object that can't have any properties, forcing TypeScript to be more strict about property access.