basarat / basarat/typescript-book

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

Open
#706 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
21.6k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

Research direction

Open the Freshness page at https://basarat.gitbook.io/typescript/type-system/freshness and review the existing explanations alongside the issue's examples. Done means the page defines Freshness and structural typing, explains structural compatibility, and makes clear why the direct object literal differs from the stored variable.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.