| ▲ | ptrl600 13 hours ago | |||||||||||||||||||||||||||||||||||||||||||
This is wordier than just "as const", what advantage does it give? (I am a newbie and genuinely don't know) edit: perhaps the advantage only comes into play for mutable values, where you want a narrower type than default, but not that narrow. Indeed, this is covered in the article, but CTRL+F "as const" doesn't work on the page for whatever reason, so I missed it. | ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | reissbaker 12 hours ago | parent | next [-] | |||||||||||||||||||||||||||||||||||||||||||
The satisfies keyword is quite different than "as const." What it does is: 1. Enforce that a value adheres to a specific type 2. But, doesn't cause the value to be cast to that type. For example, if you have a Rect type like:
You might want to enforce that some value satisfies Rect properties... But also allow it to have others. For example:
If you wrote it as:
TypeScript wouldn't allow you to also give it x and y properties. And if you did:
at the end of the line, TypeScript would allow the x, y properties, but would immediately lose track of them and not allow you to use them later, because you cast it to the Rect type which lacks those properties. You could write an extra utility type:
But that can get quite verbose as you add more fields. And besides: in this example, all we actually are trying to enforce is that the object is a Rect — why do we also have to enforce other things at the same time? Usually TS allows type inference for fields, but here, as soon as you start trying to enforce one kind of shape, suddenly type inference breaks for every other field.The satisfies keyword does what you want in this case: it enforces the object conforms to the type, without casting it to the type.
Then if someone edits the code to:
TypeScript will throw an error, since it no longer satisfies the Rect type (which wants h and w, not height and width). | ||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | bastawhiz 12 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||
I've really only found benefit on the return type of functions, when you can say that a type parameter satisfies a type (with the return type being a boolean). This let's you use `if (isPerson(foo))` and typescript will narrow the type appropriately in the conditional | ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | iddan 12 hours ago | parent | prev [-] | |||||||||||||||||||||||||||||||||||||||||||
With as const you can’t verify against another interface | ||||||||||||||||||||||||||||||||||||||||||||