Tricks I wish I knew when I learned TypeScript
1–10 of 276 posts
Re: Tricks I wish I knew when I learned TypeScript
#2Note: don't use typeof x === 'object' to check whether something is a valid object, because it will return true for arrays as well.
Arrays are objects, so this is expected behaviour.
Re: Tricks I wish I knew when I learned TypeScript
#3The third example describes something useful in record types, but goes about it in what seems an odd way, and ends up suboptimal as a result. I'd instead use an object type like this:
type Human = {
name: string;
age: number;
}
which also enforces value types in the compiler, rather than requiring runtime guards.Re: Tricks I wish I knew when I learned TypeScript
#4 interface Person {
[key: AllowedKeys]: unknown
}
This is one of annoying aspects of TypeScript. The following should work (in fact, this is how `Record` is defined in lib.es5.d.ts after all): type Person = {
[key in AllowedKeys]: unknown
};
Note the switch from interface to type and `:` replaced with `in`. The point is that the `in` syntax (conceptually expanded into multiple fields) subsumes the `:` syntax (a generic type ascription) and is only available as a mapped type, which is distinct with an interface type. The error message does mention this, but if you don't know what is the mapped type you are left with no clues.Re: Tricks I wish I knew when I learned TypeScript
#5Note: don't use typeof x === 'object' to check whether something is a valid object, because it will return true for arrays as well. Arrays are objects, so this is expected behaviour.
And it will return true for null as well ;)
Re: Tricks I wish I knew when I learned TypeScript
#6Note that `Readonly` does not prevent a call to side-effect methods when `T` is not among a predefined set of built-in types. Indeed, it prevents such calls only on predefined types such as arrays, maps, and sets. It could be more "accurate" to use `readonly number[]` instead of `Readonly>` for highlighting the difference.
Re: Tricks I wish I knew when I learned TypeScript
#7Note: don't use typeof x === 'object' to check whether something is a valid object, because it will return true for arrays as well. Arrays are objects, so this is expected behaviour.
Note: please post a better solution.
Re: Tricks I wish I knew when I learned TypeScript
#8Here's another. Instead of returning Sometype|undefined from a function which may or may not have a value to return (such as searchCustomer), return Sometype|null.
That forces the function to return a value that's explicitly intended rather than defaulting from a missed out if-else codepath. This is useful since JS is often imperative style code.
Re: Tricks I wish I knew when I learned TypeScript
#9I actually don't really like Record types in the way people/library maintainers often use them - the type-checker asserts that values are actually present for all the specified keys, which is fine if the objects with the Record type really were exhaustive; but instead I often see them used where the reality of the data is a Partial - some keys are missing. Something about the abstraction causes people to misuse it frequently.