Skip to content

Pure function validators #38346

@junaid1460

Description

@junaid1460

Search Terms

Custom type validators, pure function validators

Suggestion

having a defined way to document shape / value of a generic type like string , number. This could be a static compile time feature or it can be dragged into runtime.

Use Cases

Avoiding mistakes while creating cache keys
Validators for URLs in form of string
Validators for range

Examples

function url(urlStr: string) {
   // Test ur, if okay do nothing, else return error with examples
}

function ipAddress(ipString: string) {
   // Test ur, if okay do nothing, else return error with examples
}

const endPoint: string & ( url | ipAddress ) = "0.0.0.0"

Checklist

My suggestion meets these guidelines:

  • 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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    UnactionableThere isn't something we can do with this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions