Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Enchance typeguards to infer type based on the returned type #60522

Open
6 tasks done
Akindin opened this issue Nov 17, 2024 · 2 comments
Open
6 tasks done

Proposal: Enchance typeguards to infer type based on the returned type #60522

Akindin opened this issue Nov 17, 2024 · 2 comments

Comments

@Akindin
Copy link

Akindin commented Nov 17, 2024

πŸ” Search Terms

else typeguard, typeguard properties

βœ… Viability Checklist

⭐ Suggestion

Allow explicitly specify the type of tested value based on the type of result.

πŸ“ƒ Motivating Example

This allows to break existing types into a user defined subtypes, for example for valid | invalid strings

function isValidEmail(value: string): value is ValidEmail otherwise InvalidEmail {
    return /^\S+@\S+\.\S+$/.test(value)
}

function updateEmail(email: ValidEmail) {
    // ...
}

function showEmailFormatError(email: InvalidEmail){
    // ...
}

let email = document.querySelector("#email").value;

if (isValidEmail(email)) {
    updateEmail(email);
} else { // infers the type from otherwise
    showEmailFormatError(email)
}

Getting a response type based on a status

interface InProcessResponse {
  abort: () => void;
}

interface SuccessResponse {
  data: unknown;
}

interface ErrorResponse {
  errorCode: number;
  errorMessage: string;
}

function getStatus(response: unknown): 
    "success" means SuccessResponse |
    "pending" means InProcessResponse | 
    "error" means ErrorResponse | 
    "unknown" means unknown {
    //...
    return true
}

const response = getSomething();

switch(getStatus(response)) {
    case "success":
        // infers SuccessResponse
        break;
    case "pending":
        // infers InProcessResponse
        break;
    case "error":
        // infers ErrorResponse
        break;
    case "unknown":
        // infers unknown
        break;
}

πŸ’» Use Cases

  1. What do you want to use this for?
    To ensure handling of special cases and validation.
  2. What shortcomings exist with current approaches?
    Need of typecasting and boilerplate
  3. What workarounds are you using in the meantime?Either you make multiple typeguards Playground or you cast types before using typeguard Playground
@jcalz
Copy link
Contributor

jcalz commented Nov 17, 2024

Why does getStatus return true and not "success" etc?

otherwise: duplicate of else type guard suggessted at #15048,
means: duplicate of #31376 and/or #46650

@Akindin
Copy link
Author

Akindin commented Nov 17, 2024

Thanks for mentioning the related issues, they seem reasonably close to what I propose

Why does getStatus return true and not "success" etc?
Oh sorry that was a stub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants