3 ms·
A more robust type system can help you deal with coordination between various nooks of a codebase. For example, let's say our platform supports multiple authen
by goodoldneon 5y ago
A more robust type system can help you deal with coordination between various nooks of a codebase.
For example, let's say our platform supports multiple authentication methods. If we need to add a new auth method, how do we know all the spots we need to update? TypeScript can handle this using a sum type:
type AuthMethods = "push" | "sms" | "fancy_new_thing"
// No errors
const authMethodHandlers: {[key in AuthMethods]: CallableFunction} = {
push: () => {},
sms: () => {},
fancy_new_thing: () => {},
}
// Error: Property 'fancy_new_thing' is missing
const authMethodTitles: {[key in AuthMethods]: string} = {
push: "Push",
sms: "SMS",
}
With the above approach, static analysis tells us when we missed a spot. That adds a ton of safety and expedites feature work (since engineers don't need to manually find all the spots that need updating).