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

Make type predicates combinable with other types #45890

Closed
5 tasks done
LinqLover opened this issue Sep 15, 2021 · 2 comments
Closed
5 tasks done

Make type predicates combinable with other types #45890

LinqLover opened this issue Sep 15, 2021 · 2 comments

Comments

@LinqLover
Copy link

Suggestion

πŸ” Search Terms

TS1228, type predicate, type guards

βœ… Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Type predicates using the asserts keyword should be combinable, i.e. intersectable, unionable, etc. with other types.

πŸ“ƒ Motivating Example

Consider this snippet:

function getExtension(path: string | undefined) {
    assert(path !== undefined)
    const positionOfLastDot = path.lastIndexOf('.')
    if (!positionOfLastDot) { return path }
    return path.slice(positionOfLastDot + 1)
}

function stripExtension(path: string | undefined) {
    const extension = getExtension(path)
    return path.slice(0, path.length - extension.length - 1)
}

Currently, the compiler complains in the last line of stripExtension():

error TS2532: Object is possibly 'undefined'.
    return path.slice(0, path.length - extension.length - 1)
           ~~~~

So I need to check the type again or cast it, e.g. using path!. But this is redundant! Since we have called getExtension() before and the execution has continued here, we already know that the path exists and is not undefined. I would like to able to write a declaration l like this:

function getExtension(path: string | undefined): string & asserts path

Formally, I propose that function return types should support the combination of types that includes asserts and other type predicates (if any).

Furthermore, tsc should also be able to infer this information from the implementation of the function.

πŸ’» Use Cases

  • see example from above
  • currently, you either need to repeat your assertion or do a cast (path!.slice(...)), or in more complicated scenarios, (<string>path).slice(...))
@jcalz
Copy link
Contributor

jcalz commented Sep 15, 2021

Duplicate of #40562 for the assertion type predicate part.

I feel like I've seen discussion of non-boolean non-assertion type predicates before, but I can't find it now. Maybe #12798 (combine type predicate types) and #15048 (one-sided type predicates) and #29317 (negated types)? So (0 & x is Foo) | (1 & x is not Foo) or something?

Edit: #46650 is the currently-open issue about non-boolean type predicates.

@LinqLover
Copy link
Author

LinqLover commented Sep 15, 2021

Thank you, closing as a duplicate of #40562.

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