blog

首页 / typescript / Know How to Tell Whether a Symbol Is in the Type Space or Value Space

Know How to Tell Whether a Symbol Is in the Type Space or Value Space

A symbol in TypeScript exists in one of two spaces:

function email(to: Person, subject: string, body: string): Response {
 //      ――――― ―――――        ―――――――          ――――                     Values
 //               ――――――            ――――――        ――――――   ――――――――   Types
}

There are many other constructs that have different meanings in the two spaces:

Correct

function email(options: {to: Person, subject: string, body: string}) {
 // ...
}
function email(
 {to, subject, body}: {to: Person, subject: string, body: string}
) {
 // ...
}

ERROR

function email({
 to: Person,
 // ~~~~~~ Binding element 'Person' implicitly has an 'any' type
 subject: string,
 // ~~~~~~ Binding element 'string' implicitly has an 'any' type
 body: string
 // ~~~~~~ Binding element 'string' implicitly has an 'any' type
}) { /* ... */ }

Things to Remember