57 points · by luu · 3y ago · 56 comments
const base: BaseClass = new SubClass();
const keys = Object.keys(base); // should give you the subclass keys as well
(This is pseudo-code that might not quite work due to how prototypes and classes interact, but I'm trying to illustrate a principle.)What's perhaps a bit surprising is that all types in TypeScript behave like BaseClass in this example. Object types (e.g. {a: number, b: string}) specify the minimally required properties, not the maximally allowed properties -- they are effectively the same thing as interfaces. Any concrete object that implements the interface can have more properties than the interface requires.
That's so fundamental to the type system that I think if you want to work around it, you might not fully understand how TypeScript works yet.
I think most engineers who use (and swear by) TypeScript don't fully understand how TypeScript works (or JavaScript for that matter).
I've worked across multiple teams at multiple companies and always run into types I can't trust because they are just plain wrong and inconsistent with the runtime, or data that isn't typed correctly. I also found that everyone writes TS differently, and sometimes the verbose typings and weird non-idiomatic patterns introduced just to please tsc actually make the code harder to read (30-line JS modules become 120-line TS behemoths). I still prefer working with TS on teams (though I prefer JS on solo projects now), but after a while I ask "is this the best we can do?"
/end minirant
In contrast, OCaml's objects differs <a: int; b: int> (exactly those fields) from <a: int; b: int; ..> (may have more fields). Likewise for Purescript records etc
This stuff was figured out decades ago. There was no need for Typescript to be like this
typescript is strictly a superset of javascript, and javascript is like this.
I think it is also a large part of the reason why TypeScript is amazing and very useful in the real world.
TS is very pragmatic in the way it allows you to convince it you know what you're doing and, really, this will be fine in reality even if the types don't really agree. It's a DX tradeoff I believe is usually worth making.
But it is comical on a certain level how you'll choose to use a tool to point out the potential of problems with your code but then sometimes ignore it / tell it it is wrong when it does :-)
Usually when the tool fails to be expressive enough for me to comfortably communicate that no, it’s definitely going to be T, not `string`
Using Object.entries/keys/fromEntries and Array.reduce are my least-favourite moments with TS because of how darn wordy and assertive I have to get.
I disagree here. Typescript allows you to lie about your code, and that's only a ticking time bomb that will work until it doesn't. The workaround might work in the current state of your application, but over time if things/people change, it makes it much more difficult to reason about things.
Because typescript allows interfaces to have additional properties, five levels up the call stack someone will pass in an object that has extra properties for whatever reason, and then the assumptions we once made about the code fall out and you ship an unknown bug.
Of course, you'd run into the same issue in regular Javascript by expecting a base class and receiving a child class of that base.
I've worked on many dynamically types systems and have no problems thinking in terms of compile time, run time, and macro expansion if you're so lucky, but some people just want the computer to say yes.
Oh and miss specification of the system will happen in all. Even when computer says yes.
What I don't understand is why you would chose to use Typescript, and then opt out of the only/main benefit it actually has. At that point you're just writing dynamically typed Javascript with more keyboard presses.
I could just as easily argue with equivalent experience the exact opposite.
That's a terrifyingly vague comment!
knownKeys(o, ["x", "y"]); // returns ["x", "y"]
Excess keys are not an issue, because they are ignored. And if I fail to list a key that the compiler knows about, the compiler will complain. const taskCounts = { work: 10, home: 5, personal: 3 };
This probably has the deduced type of `Record<"work" | "home" | "personal", number>`.I wonder if there is a hole in the type system that could be filled with an `ExactKeyTypeRecord<K, V>`, which is invariant in K and is a subtype of `Record<K, V>`. Maybe this could have Object.keys() defined as expected.
I have no experience with TS, but I believe Python's TypedDict has a bunch of similar holes in the type system that could be mitigated with a bit more elaborate type hierarchies.
const taskCounts = { work: 10, home: 5, personal: 3 };
const what : Record<string, number> = taskCounts; // OK, covariance?
const the : Record<"work" | "home", number> = taskCounts; // Also OK, contravariance?
const hell : Record<"work" | "home" | "personal" | "hello", number> = taskCounts; // This is an error
https://www.typescriptlang.org/play?#code/MYewdgzgLgBFCGEDWB...This really needs to be stated more clearly I guess.
Object.keys(someObject).reduce((acc, key) => ...., { const val = someObject[key] })
When I extract the value in Typescript, I always have to coerce the type like: someObject[key as keyof typeof someObject]
It would be nice if the behaviour with "as const" was different:
const person = { name: 'John Doe', age: 35 } as const
const result = Object.keys(person) // return type is not 'name' | 'age'> 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()) let foo: number;
because at runtime you can assign a string to foo.Typescript's type system is for compile-time checking, not runtime checking. The above definition is correct if at compile-time you want to check that all assignments to foo are of type number.
That's why I occasionally list specific options in addition to the larger parent type, just for the IDE, not for the type checks.
TypeScript mixes a lot of things and tools that are actually separate things and presents it in one big package, for better or worse, not just in this case.
(keyof typeof someObject | (string & Record<never, never>))[]
which will give you autocomplete but still allow arbitrary strings. TS will still complain as soon as you use it as an object indexer though, defeating the point.Or just don't do it, it's not type safe.
Very interesting though, I have not thought that it could be a problem/necessity, maybe I'm programming on "automatic mode", in a way that I already know the workarounds of TS type system.