You’re right about how it works today but it seems to me this is an odd choice to make. Why not enforce object types?
Could they add a strictness flag that lets objects actually be type safe and enables related functions to work out of the box? For me this stuff is a regular pain point as you end up typecasting at every call site :(
function sortByLabel(a: SortableByLabel, b: SortableByLabel) { ... }
I've written enough, I think you can see how easily this works today and how painful and un-performant it would be to do this if you had to narrow your objects or constantly write extra info into your types to allow them to not enforce narrowing, and then there's a whole can of worms around your "widen"-able object types being illegal to functions that require exact types, ...
Instead you can just write arrays...
interface MyInterface {
key1: Type1;
key2: Type2;
...
}
const myKeys:Array<keyof MyInterface> = [
'key1',
'key2',
...
] as const;
function operatesOnKeys(object: MyInterface) {
// don't use Object.entries...
myKeys.forEach((key) => {
object[key];
// TS is happy, and bonus:
// this will be type checked later if MyInterface changes
});
}
I think TS has some issues but this isn't one of them.
Could they add a strictness flag that lets objects actually be type safe and enables related functions to work out of the box? For me this stuff is a regular pain point as you end up typecasting at every call site :(