-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Aggregate function assertions #42253
Comments
Update, just figured out that the following issues already cover the second part of what I'd have like see in TS,
I guess this should be scoped solely to returning multiple |
I also need multiple asserts guard type. Here is my Use Case: I have a class Application (or any class that aquires resources). When is properly connected the class have a database opened connection, a path to class Application{
database Database | null = null
log Log | null = null
tmpPath string | null = null
async connectToDb(conn:string){
this.database = pg.connect(conn);
}
async setPath(path:string){
....
}
async logTo(path:string){
....
}
isProperlyConnected(database:Database|null, log:Log|null, path:string|null) asserts database is Database & log is Log & path is string{
if(databse == null) throw new Error('database is null');
...
}
async doStuff1(){
this.isProperlyConnected(this.database, this.log, this.tmpPath);
this.database.query('select ....') ...
}
async doStuff2(){
this.isProperlyConnected(this.database, this.log, this.tmpPath);
this.log.redirectTo(this.tmpPath) ...
}
} What are the adventages? If in the feature I need to add other resource to isProperlyConnected I do it and I warn if I forget to change it un some stuff. But if I call separate guards for each resource I do not warn if I forget one. |
Also wrapping individual assertion functions doesn't work. The assertion doesn't seem to be passed up transitively. function assertNumber(value: unknown): asserts value is number {
if (typeof value !== 'number') throw new Error('expected value to be a number')
}
// this doesn't work
function assertBothNumber(x: unknown, y: unknown) {
assertNumber(x)
assertNumber(y)
} Perhaps it is an easier way forward to propagated assertion functions? |
@Pyrolistical Ideally your example of |
@phosra I agree, but perhaps the desired behaviour can be delivered without a syntax change. That would reduce the scope. |
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.) |
Suggestion
π Search Terms
Multi-assertions, function assertion signatures, type assertions, aggregate assertions
β Viability Checklist
My suggestion meets these guidelines:
β 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 thanvoid
.(Update: the secondary goal is covered by other issues)
π» Use Cases
It allows multiple type assertions at once.
or
or
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.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:
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?
#40562
#34636
The text was updated successfully, but these errors were encountered: