Item 39: Prefer Unifying Types to Modeling Differences
要点
- Having distinct variants of the same type creates cognitive overhead and requires lots of conversion code.
- Rather than modeling slight variations on a type in your code, try to eliminate the variation so that you can unify to a single type.
- Unifying types may require some adjustments to runtime code.
- If the types aren't in your control, you may need to model the variations.
- Don't unify types that aren't representing the same thing.
- 相同类型的不同变体会增加认知负担,并需要大量的转换代码。
- 与其在代码中建模类型的轻微变化,不如消除这些变化,以便将它们统一为单一类型。
- 统一类型可能需要对运行时代码进行一些调整。
- 如果这些类型不在你的控制之下,可能需要对变体进行建模。
- 不要统一那些不代表相同事物的类型。
正文
ts
interface StudentTable {
first_name: string
last_name: string
birth_date: string
}
ts
interface Student {
firstName: string
lastName: string
birthDate: string
}
ts
type Student = ObjectToCamel<StudentTable>
// ^? type Student = {
// firstName: string;
// lastName: string;
// birthDate: string;
// }
ts
async function writeStudentToDb(student: Student) {
await writeRowToDb(db, 'students', student)
// ~~~~~~~
// Type 'Student' is not assignable to parameter of type 'StudentTable'.
}
ts
async function writeStudentToDb(student: Student) {
await writeRowToDb(db, 'students', objectToSnake(student)) // ok
}