79451783

Date: 2025-02-19 14:51:57
Score: 0.5
Natty:
Report link

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.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: ksantos