Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a prefixing "&" mean in a Typescript type definition?

Tags:

typescript

From DefinitelyTyped:

export type CSSProperties = & ObservableProperties & ObservableProperties;

Every description of ampersand that I can find says that it is an intersection operator. In which case, what does the first ampersand mean?

References:

  • What does the ampersand (&) mean in a TypeScript type definition?

  • Official doc

  • Typescript Deep Dive

like image 952
Ed Staub Avatar asked Oct 18 '25 23:10

Ed Staub


1 Answers

Leading ampersand is ignored by the current version of the compiler, pretty much in the same way as the last ; is ignored inside interface declaration:

interface a {
    a(): string;
}


type b = & a;

TypeScript playground says type b = a

The typings linked from the question are (ab)using this feature to have more uniform syntax for intersection types:

export type CSSProperties =
    & ObservableProperties<csstype.Properties>
    & ObservableProperties<csstype.PropertiesHyphen>;

When written this way, when you remove the first intersection member for example, you can just delete the whole line and you don't have to change anything else.

like image 147
artem Avatar answered Oct 20 '25 12:10

artem