Skip to content

TypeScript compatible syntax #5

@phaux

Description

@phaux

🔍 Search Terms

✅ Viability Checklist

⭐ Suggestion

Current syntax is an error in TS. Support a syntax which is interpreted as a throw type annotation in ErrorScript but ignored in TS. For example:

// Definition for TS, ignored by ErrorScript
namespace ErrorScript {
  type Result<T, E> = T // This type is valid only as a return type
  type Promise<T, E> = Promise<T>
}

function maybeGetNumber(): ErrorScript.Result<number, DOMException> {
  const n = Math.random()
  if (n < 0.5) throw new DOMException("Too low", "RandomError")
  return n
}

async function maybeGetNumberLater(): ErrorScript.Promise<number, DOMException> {
  const n = Math.random()
  if (n < 0.5) throw new DOMException("Too low", "RandomError")
  return n
}

Additionally, for JS with JSDoc, a @throws tag could be used:

/**
 * @returns {number}
 * @throws {DOMException}
 */
function maybeGetNumber() {
  const n = Math.random()
  if (n < 0.5) throw new DOMException("Too low", "RandomError")
  return n
}

Also it would be worth adding a JS mode to the playground so the JSDoc support can be tested too.

📃 Motivating Example

💻 Use Cases

Easier migration. Not everybody has to switch at once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions