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

Request: option for refinement by console.assert #6729

Open
espretto opened this issue Aug 12, 2018 · 0 comments
Open

Request: option for refinement by console.assert #6729

espretto opened this issue Aug 12, 2018 · 0 comments

Comments

@espretto
Copy link

espretto commented Aug 12, 2018

Hello,

would it be feasable to add an option giving console.assert the same semantics as throw-statements with regard to refinements? The below example currently points out that .parentNode may be undefined:

function remove(node: Node): Node {
  return node.parentNode.removeChild(node)
}

Inserting an if-throw/-return statement refines the type of .parentNode for the else-branch. The type-checker is happy with this:

function remove(node: Node): Node {
  if (node.parentNode == null) throw new Error('cannot remove orphaned node')
  return node.parentNode.removeChild(node)
}

Although console.assert does not change execution flow, the intend is otherwise the same. In addition, console-statements can be stripped once the tests pass.

function remove(node: Node): Node {
  console.assert(node.parentNode, 'cannot remove orphaned node')
  return node.parentNode.removeChild(node)
}

This of course assumes, the application is not supposed to recover from such an error.

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

No branches or pull requests

2 participants