Skip to content

Nominal typeΒ #62495

@achaozuiq

Description

@achaozuiq

πŸ” Search Terms

"type","Structured","alias"

βœ… Viability Checklist

⭐ 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions