question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Aggregate function assertions

See original GitHub issue

Suggestion

🔍 Search Terms

Multi-assertions, function assertion signatures, type assertions, aggregate assertions

✅ 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

Allow functions that assert a type or condition to assert multiple types or conditions

(Maybe?) Allow functions with “asserts” signatures to return a type other than void. (Update: the secondary goal is covered by other issues)

💻 Use Cases

It allows multiple type assertions at once.

// declare AssertionError

function assert(x: unknown, y: unknown): (asserts x is number) & (asserts y is number) {
    if (typeof x !== "number" || typeof y !== "number") {
        throw new AssertionError("Assert failed: 'x' and 'y' are not both numbers");
    }
}

or

function safeAdd(x: unknown, y: unknown): (number) & (asserts x is number) & (asserts y is number) {
    if (typeof x !== "number" || typeof y !== "number") {
        throw new AssertionError("`x` and `y` are not both numbers");
    } else {
        return x + y;
    }
}

or

declare function safeAdd(...args: unknown[]): asserts args is [number, number];

No idea what the syntax would be, but this should give the idea.

📃 Motivating Example

My use for this was that I had a function that accepted two parameters. It only operated if the first parameter was of a type (T), it also had an overload for the second parameter being the same type (T) or anything else.

declare function assertIsT (x: unknown): asserts x is T;
declare function isT (x: unknown): x is T;
function f (x: unknown, y: unknown): asserts x is T;
function f (x: T, y: T | H) {
    assertIsT(x);
    // x: T

    if ( isT(y) ) {
        // ... y: T
    } else {
        // ... y: H
    }
}

This was meant to be “JavaScript-compatible” code, as in, it could be used by a non-TS user with relative type safety, but, the assertion seems like extra work, as if I’m being over-cautious, so I wanted to simplify it:

declare function isT (x: unknown, y: unknown): (asserts x is T) & (y is T);
function f (x: unknown, y: unknown): asserts x is T;
function f (x: T, y: T | H) {
    if ( isT(x, y) ) {
        // ... x: T
        // ... y: T
    } else {
        // ... x: T
        // ... y: H
    }
}

Now, it’s terse and straightforward, while retaining type-safety. (I am not advocating any specific syntax for the aggregation/mixing)

I tried to get this to work with function overloads, but it wasn’t happening. Playground link


Related issues? https://github.com/microsoft/TypeScript/issues/40562 https://github.com/microsoft/TypeScript/issues/34636

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:18
  • Comments:6

github_iconTop GitHub Comments

1reaction
Pyrolisticalcommented, Jul 24, 2022

@Phosra I agree, but perhaps the desired behaviour can be delivered without a syntax change. That would reduce the scope.

0reactions
sebinsuacommented, Oct 21, 2022

I would also like this.

My use case is wanting to be able to narrow multiple arrays into the same opaque/branded/flavoured type when they are of equal length. This would help us to create type-safe data operations for manipulation of arrays and matrices, etc.

I agree with @Pyrolistical that propagating type assertions would allow us to compose them and might be preferable to creating further syntax. (I also tried doing this naturally, before realising it wasn’t supported.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overview of Aggregate Functions | InterSystems SQL Reference
An aggregate function performs a task in relation to one or more values from a single column and returns a single value. The...
Read more >
Plansplaining, part 1. The unexpected aggregation and assert
ANY is an aggregate function that we cannot use in our T-SQL, but the optimizer can use it in execution plans; it means...
Read more >
Assertions — PHPBench documentation
PHPBench provides the aggregated iteration results for the current variant and, if a reference is specified, the baseline . /** * @Assert("mode(variant.time.avg) ...
Read more >
Assertive R Programming with assertr
success, error and defect functions · success_continue - just returns the data that was passed into the assertion function (this is default).
Read more >
Unique aggregate function when singular value is guaranteed ...
Ideally I would have wanted a UNIQUE aggregate function which would assert that all values are equal and return that single value, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found