Skip to content

Answer value.constructor.name for class instances and errors - #1483

Open
ctate wants to merge 2 commits into
mainfrom
ctate/answer-value-constructor-a84cbd
Open

ctate wants to merge 2 commits into
mainfrom
ctate/answer-value-constructor-a84cbd

Conversation

@ctate

@ctate ctate commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator
  • value.constructor.name compiles in TypeScript for class instances and errors, and names the class that constructed the value as Node does. Program classes without subclasses fold to a constant.
  • Caught builtin errors in JavaScript answer their class (RangeError, DOMException) instead of undefined.

value.constructor.name names the class that constructed the value, as in
Node:

- A TypeScript program class without subclasses answers its name at
  compile time, as C.name does. Instances of classes with subclasses read
  constructor and name through the instance's checked-dynamic view.
- An error-typed value tests the program's error classes that extend its
  static class, most derived first, and otherwise answers the native
  error's class (RangeError, DOMException, ...). An assigned name property
  does not change the result.
- An untyped value reads constructor and then name. A value that holds a
  native error answers the error's class, so a caught RangeError answers
  "RangeError" instead of undefined. A value whose constructor is
  undefined or null throws Node's TypeError for the name read.

Previously the TypeScript form was refused with SC2020.
@vercel

vercel Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
scriptc Ready Ready Preview, v0 Oct 20, 2026 5:33pm UTC

Comment thread packages/compiler/src/frontend/lowering/constructor-name.ts Outdated
Comment on lines +7164 to +7172
ScrError *error = scr_errdyn_err_of(value);
if (error) {
ScrStr *class_name = scr_error_class_name(error);
scr_error_release(error);
ScrDyn *out = scr_dyn_new_str(class_name);
scr_str_release(class_name);
return out;
}
if (constructor->kind == SCR_DYN_UNDEF || constructor->kind == SCR_DYN_NULL) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an untyped error's constructor is replaced with null or an object whose name is not a string, the fallback returns the error's builtin class name instead of throwing TypeError or returning the actual name value.

Fix on Vercel

This branch was successfully deployed

1 active deployment
Preview — 4a5db611 Deployed Oct 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant