microsoft / microsoft/TypeScript

Docs: "Type assertion" vs "asserts" keyword

Open
#35,653 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Discussion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Hey there!

I’m updating Programming TypeScript to include assertions in control flow analysis, and was looking for guidance about naming.

Type assertions (x as T) and assertions in control flow analysis (asserts x is T) are similarly named. What’s a good way to call these these features, in a way that doesn’t confuse people and aligns with the way the TS team is communicating it?

A couple of ideas:

  1. Rename x as T to “type coercion” or “type casting" (even though it’s not a runtime behavior), and call asserts x is T a “type assertion”
  2. Keep x as T as-is (“type assertion”), and call asserts x is T a “user-defined type assertion”, similar to a “user-defined type guard”

Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked pull request 32695 and the issue’s discussion of x as T versus asserts x is T. Done means reaching an agreed naming convention that aligns with TypeScript’s communication and can guide the Programming TypeScript update.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.