-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Description
π Search Terms
"type","Structured","alias"
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
Provides a simple keyword for creating non confusing type aliases.
π Motivating Example
Say goodbye to the tedious "brand type" hack
π» Use Cases
// Error Examples
type UserID = string;
type PostID = string;
function deleteUser(id: UserID) { /* ... */ }
const myPostID: PostID = "post-123";
deleteUser(myPostID); // It's perfectly legal! But logically, it is catastrophic.
// Solution
// Scenario 1: Use the keyword "nominal"
nominal type UserID = string;
nominal type PostID = string;
// Scheme 2: Rust like newtype
type UserID = newtype string;
//Create non confusable type aliases
// At the developer level, it is really wrong. Two types of "shapes" are the same and cannot be assigned to each other. If their names are different
On the logic level, as long as two types of "shapes" are the same, they can assign values to each other, no matter what their names are
Now we need to solve this problem!