TypeScript-Handbook icon indicating copy to clipboard operation
TypeScript-Handbook copied to clipboard

"object structure typing" syntax is never explicitly introduced

Open abrindam opened this issue 6 years ago • 0 comments

This is really basic, but it actually tripped me up for a bit. TypeScript allows you to declare the structure of an object type without declaring an interface, as in the type of "labelledObj" in this example from the Interfaces page:

function printLabel(labelledObj: { label: string }) {
    console.log(labelledObj.label);
}

The Interfaces page uses this syntax offhandedly, but it's never actually introduced or even given a name (which is why I've made up the term "object structure typing" which I'm sure is wrong).

Perhaps I'm an idiot, but as a result, I actually didn't realize you could do this. I thought you had to declare a Class or Interface for any structured type you wanted to specify!

It seems like this syntax should be introduced in Basic Types.

Alternatively, it could be explicitly introduced in Interfaces before its current content. However, I favor Basic Types because it makes sense to introduce this syntax before Destructuring/Spread which is discussed before Interfaces in Variable Declarations

abrindam avatar Jan 06 '19 23:01 abrindam