You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
type Colors = "red" | "green" | "blue";
type RGB = [red: number, green: number, blue: number];
const palette: Record<Colors, string | RGB> = {
red: [255, 0, 0],
green: "#00ff00",
bleu: [0, 0, 255]
// ~~~~ The typo is now correctly detected
};
// But we now have an undesirable error here - 'palette.red' "could" be a string.
const redComponent = palette.red.at(0);
since values are unions of string and array, palette.red.at(0) will not throw an error, because at method is a method that exists for both types.
Cheers :).
I think OP's point is that the docs say that there is an "undesirable error" on palette.red.at(0), but there's no error. Maybe the word "error" is wrong, or maybe the docs should use an array-only method like map() or something like it instead of at().
There's no error in the docs, just poor wording when it mentions an "error". Using map wouldn't showcase the example they're making. With a type annotation the return type of at() is string | number, but when using satisfies the return type is number.
TypeScript developers are often faced with a dilemma: we want to ensure that some expression matches some type, but also want to keep the most specific type of that expression for inference purposes.
For example:
// Each property can be a string or an RGB tuple.constpalette={red: [255,0,0],green: "#00ff00",bleu: [0,0,255]// ^^^^ sacrebleu - we've made a typo!};// We want to be able to use string methods on 'green'...constgreenNormalized=palette.green.toUpperCase();
Notice that we've written bleu, whereas we probably should have written blue.
We could try to catch that bleu typo by using a type annotation on palette, but we'd lose the information about each property.
typeColors="red"|"green"|"blue";typeRGB=[red: number,green: number,blue: number];constpalette: Record<Colors,string|RGB>={red: [255,0,0],green: "#00ff00",bleu: [0,0,255]// ~~~~ The typo is now correctly detected};// But we now have an undesirable error here - 'palette.greed' "could" be of type RGB and// property 'toUpperCase' does not exist on type 'string | RGB'.constgreenNormalized=palette.green.toUpperCase();
The new satisfies operator lets us validate that the type of an expression matches some type, without changing the resulting type of that expression.
As an example, we could use satisfies to validate that all the properties of palette are compatible with string | number[]:
typeColors="red"|"green"|"blue";typeRGB=[red: number,green: number,blue: number];constpalette={red: [255,0,0],green: "#00ff00",bleu: [0,0,255]// ~~~~ The typo is now caught!}satisfiesRecord<Colors,string|RGB>;// toUpperCase() methode is still accessible!constgreenNormalized=palette.green.toUpperCase();
satisfies can be used to catch lots of possible errors.
For example...
Activity
MartinJohns commentedon Jan 10, 2024
The point is not a missing
at()method, but that it may return astring. There's no error her, just a misunderstanding.sivharinair2001 commentedon Jan 10, 2024
There will be no error in palette.red.at(0) as TypeScript should be able to correctly infer the type based on the property access.
jcalz commentedon Jan 10, 2024
I think OP's point is that the docs say that there is an "undesirable error" on
palette.red.at(0), but there's no error. Maybe the word "error" is wrong, or maybe the docs should use an array-only method likemap()or something like it instead ofat().Anyway I think doc errors tend to belong at https://github.com/microsoft/TypeScript-Website/issues
MartinJohns commentedon Jan 10, 2024
There's no error in the docs, just poor wording when it mentions an "error". Using
mapwouldn't showcase the example they're making. With a type annotation the return type ofat()isstring | number, but when usingsatisfiesthe return type isnumber.RyanCavanaugh commentedon Jan 11, 2024
I think the example was unintentionally written against a pre-
String#attargetmariusgau commentedon Mar 27, 2024
I believe the documentation is incorrect or misleading. I can send a PR to https://github.com/microsoft/TypeScript-Website if wanted.
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html#the-satisfies-operator should look like:
The
satisfiesOperatorTypeScript developers are often faced with a dilemma: we want to ensure that some expression matches some type, but also want to keep the most specific type of that expression for inference purposes.
For example:
Notice that we've written
bleu, whereas we probably should have writtenblue.We could try to catch that
bleutypo by using a type annotation onpalette, but we'd lose the information about each property.The new
satisfiesoperator lets us validate that the type of an expression matches some type, without changing the resulting type of that expression.As an example, we could use
satisfiesto validate that all the properties ofpaletteare compatible withstring | number[]:satisfiescan be used to catch lots of possible errors.For example...
RyanCavanaugh commentedon Mar 27, 2024
Looks nice, please go ahead with the PR. Thanks!
pieterocp commentedon Mar 12, 2025
This looks to have been merged into the typescript docs so can probably be closed?