TS: Parser error with assertion signatures #41

Closed
opened 2024-12-02 15:19:55 +01:00 by SirPepe · 5 comments
SirPepe commented 2024-12-02 15:19:55 +01:00 (Migrated from github.com)

Returns types for assertion functions lead to parse errors:

function assert(condition: any): asserts condition {
  if (!condition) {
    throw new Error();
  }
}

function assertIsString(val: any): asserts val is string {
  if (typeof val !== "string") {
    throw new Error("Not a string!");
  }
}

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABAQwM6oKYCcoAoIIAmMsCAXCmAJ4CUFamOqiBYxpSA3gFCKIzBEuAISt28MDUQ8+fKAAsscAO6IwGVQFEsSrLhoBuXogC+3M91CQOKdNigBJVAGUoWGGADmuAG7IANvTUdLaMUMx+-vzMqG4entLGAkJQVAAOGHCCkYjCALx5iABEse5eRVIysgpKqupaOnB6RQBycFAoiKXxwhVGfGYWhBgQ-shYGIj+GB3AcHBBVADaALqIAD5dcV4baiD+-kbcDPa4c3CG3JbzBogA9HdbZQmbyNSrVyc4Tq7PZ-OXa5wW4PJ7xIA

Returns types for assertion functions lead to parse errors: ```typescript function assert(condition: any): asserts condition { if (!condition) { throw new Error(); } } function assertIsString(val: any): asserts val is string { if (typeof val !== "string") { throw new Error("Not a string!"); } } ``` https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABAQwM6oKYCcoAoIIAmMsCAXCmAJ4CUFamOqiBYxpSA3gFCKIzBEuAISt28MDUQ8+fKAAsscAO6IwGVQFEsSrLhoBuXogC+3M91CQOKdNigBJVAGUoWGGADmuAG7IANvTUdLaMUMx+-vzMqG4entLGAkJQVAAOGHCCkYjCALx5iABEse5eRVIysgpKqupaOnB6RQBycFAoiKXxwhVGfGYWhBgQ-shYGIj+GB3AcHBBVADaALqIAD5dcV4baiD+-kbcDPa4c3CG3JbzBogA9HdbZQmbyNSrVyc4Tq7PZ-OXa5wW4PJ7xIA
marijnh commented 2024-12-02 15:27:50 +01:00 (Migrated from github.com)

I wasn't even aware that syntax existed. Attached patch adds support.

I wasn't even aware that syntax existed. Attached patch adds support.
SirPepe commented 2024-12-03 12:26:50 +01:00 (Migrated from github.com)

asserts x is y works now, but the following still gives me a parse error:

function assert(condition: any): asserts condition {
  if (!condition) {
    throw new Error();
  }
}
Script(FunctionDeclaration(function,VariableDefinition,ParamList("(",VariableDefinition,TypeAnnotation(":",TypeName),")"),TypePredicate(":",asserts,VariableName,⚠),Block("{",IfStatement(if,ParenthesizedExpression("(",UnaryExpression(LogicOp,VariableName),")"),Block("{",ThrowStatement(throw,NewExpression(new,VariableName,ArgList("(",")")),";"),"}")),"}")))
`asserts x is y` works now, but the following still gives me a parse error: ```typescript function assert(condition: any): asserts condition { if (!condition) { throw new Error(); } } ``` ``` Script(FunctionDeclaration(function,VariableDefinition,ParamList("(",VariableDefinition,TypeAnnotation(":",TypeName),")"),TypePredicate(":",asserts,VariableName,⚠),Block("{",IfStatement(if,ParenthesizedExpression("(",UnaryExpression(LogicOp,VariableName),")"),Block("{",ThrowStatement(throw,NewExpression(new,VariableName,ArgList("(",")")),";"),"}")),"}"))) ```
marijnh commented 2024-12-03 12:34:03 +01:00 (Migrated from github.com)

All the examples of this I could find used is. I cannot find the feature at all in the official docs. Do you have any resource that explains it properly?

All the examples of this I could find used `is`. I cannot find the feature at all in the official docs. Do you have any resource that explains it properly?
SirPepe commented 2024-12-03 12:42:34 +01:00 (Migrated from github.com)

It's mentioned explicitly in the release notes for the relevant version: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions

I have never seen it mentioned anywhere else or used in any code, but I believe this is intended as an official feature.

It's mentioned explicitly in the release notes for the relevant version: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions I have never seen it mentioned anywhere else or used in any code, but I believe this is intended as an official feature.
marijnh commented 2024-12-03 12:57:42 +01:00 (Migrated from github.com)

Hrm, mentioning something in a release note and never documenting it is hardly a way to run a software project, if you ask me. But attached patch adds this form.

Hrm, mentioning something in a release note and never documenting it is hardly a way to run a software project, if you ask me. But attached patch adds this form.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
lezer/javascript#41
No description provided.