Skip to content

Incomplete documentation of "Freshness" and "Structural typing" on https://basarat.gitbook.io/typescript/type-system/freshness #706

Open
@AnupamKhosla

Description

@AnupamKhosla

It is never defined or explained, what actually "Freshness" and "Structural Typing" are.

TypeScript provides a concept of Freshness (also called strict object literal checking) to make it easier to type check object literals that would otherwise be structurally type compatible.

Structural typing is extremely convenient. Consider the following piece of code. This allows you to very conveniently upgrade your JavaScript to TypeScript while still preserving a level of type safety:

But, what is the concept of Freshness? What does it mean to be structurally compatible and what is structural typing?

The correct explanation is

function logName(x: { name: string }) {
    console.log(x.name);
}

const person = { name: "Alice", age: 30 };
logName(person); // ✅ OK — structurally compatible. 
logName({ name: "Bob", age: 30 }); // ❌ Error -- Freshness; passed a fresh object

Freshness means no extra properties are allowed if an object literal is passed straight away.

Structurally compatible means if the object is first stored in a var, it can be passed with extra properties.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions