3 ms·
In all of your examples you're either starting from `any` or doing a type assertion somewhere along the line. That's explicitly opting-out of type checking, the
by matt_kantor 3y ago
In all of your examples you're either starting from `any` or doing a type assertion somewhere along the line. That's explicitly opting-out of type checking, the same way that asserting an `Object.keys` return value is `(keyof T)[]` would be.
> I feel like it just makes more sense to trust that the object type is what we've said it is
The object type is what we've said it is. The type is correct, it just doesn't necessarily specify every last detail of your value, because subtyping is a thing. For example:
class Pet { name: string }
class Dog extends Pet { isAGoodBoy: boolean }
const f = (pet: Pet): keyof Pet => Object.keys(pet)
// this call should be allowed, but that means `f`'s return type is wrong
f(new Dog())