Forbidden constructs (Phase 0 validator)

src/validator.ts walks the whole syntax tree before the checker and rejects everything below on the first hit, regardless of where it appears (even in dead code or nested type arguments). Each row gives the exact message fragment tests/run.js matches and the case that proves it.

Types

Construct Message Test
any anywhere, including nested type arguments `any` is forbidden in Nish reject_any_param, reject_any_nested
unknown `unknown` is forbidden in Nish reject_unknown_param, reject_unknown_nested
symbol type `symbol` type is forbidden in Nish (no symbol type) reject_symbol_type
bigint type `bigint` type is forbidden in Nish (use number, i32, or f64) reject_bigint_type
undefined type `undefined` is forbidden in Nish; use `null` with a `T | null` type reject_union_undefined (via the union rule)
union types other than T | null / null | T and discriminated unions Union types other than `T | null` are forbidden in Nish (values have one fixed layout) reject_union_type, reject_union_undefined, reject_union_untagged_scalar
Function, Symbol, Proxy as type names `Function` type is forbidden in Nish (no dynamic function values) (and analogous) reject_function_type
import("x").T Dynamic `import()` is forbidden in Nish (modules are resolved at compile time) reject_dynamic_import
x as any, <any>x Type assertion to `any` is forbidden in Nish reject_as_any, reject_as_any_angle
x as unknown, <unknown>x Type assertion to `unknown` is forbidden in Nish reject_as_unknown

Declarations

  • type parameters on a type alias: Generic type parameters are forbidden on a type alias in Nish; a generic function, class or interface is monomorphised, and an alias only renames a type that already exists (reject_type_alias_generic)
  • type parameters on a constructor: a constructor cannot have type parameters: it takes its class's, which are written after `new` (`new Box<i32>(v)`) (a syntax error; a method may declare its own, see Generic methods) (reject_generic_method_constructor)
  • a generic class or interface named without its type arguments: `Holder` is generic: it must be written with its type arguments, e.g. `Holder<number>` (reject_generic_class)
  • type arguments on new of a class that is not generic: `Point` is not generic, so `new Point` takes no type arguments (reject_cls_new_generic)
  • generators (function*, *m()): Generators are forbidden in Nish (no coroutine runtime) (reject_generator, reject_generator_method)
  • an async method, field or constructor (Async functions has the rest): Only a top-level function or an arrow may be `async` (NL1063) (reject_async_method)
  • var: `var` is forbidden; use `let` or `const` (reject_var_keyword, reject_var_in_for)
  • enum members that are neither all numeric nor all string literals: Enum members must be numeric literals in Nish, or every member a string literal (an enum lowers to a plain integer, and one numbering cannot mix with strings) (NL1043, reject_enum_string_mixed, reject_enum_computed; the numeric and the string forms compile (Enums))
  • namespace / module blocks, declare module "m": `namespace` and `module` blocks are forbidden in Nish (use ES module files) (reject_namespace, reject_module_declared)
  • declare global: `declare global` is forbidden in Nish (no global object to augment) (reject_declare_global)
  • decorators, on a class, a member or a parameter: Decorators are forbidden in Nish (no runtime metadata or class rewriting) (reject_decorator, reject_decorator_member, reject_decl_shapes)
  • computed property names, in an object literal ({ [k]: 1 }, { [k]() { } }), a class ([k]: i32, [Symbol.iterator]() { }) or an interface ([k]: i32): Computed property names are forbidden in Nish (member names are fixed at compile time) (NL1041, Phase 0; before a method in an object literal is refused) (reject_computed_property, reject_computed_method, reject_cls_computed_field, reject_cls_computed_method, reject_iface_computed_member)

Imports

Construct Message Test
import attributes, with { type: "json" } or assert { ... } Import attributes (`with { ... }`) are forbidden in Nish (an import names source this compiler reads, never a resource loaded at run time) reject_import_attributes, nl1057_import_attributes
a module export name written as a string, import { "a-b" as b } A module export name written as a string is forbidden in Nish (an export is a declaration, and a declaration has a name; import it by that name) reject_import_string_name, nl1058_string_export_name
import defer `import defer` is forbidden in Nish (a module has no top-level code, so there is nothing to defer) reject_import_defer, nl1059_import_defer

Statements

  • with: `with` is forbidden in Nish (no dynamic scope) (reject_with_statement)
  • try / catch / finally: `try`/`catch`/`finally` is forbidden in Nish (no unwinding; use `Result<T, E>`) (reject_try_catch)
  • throw: `throw` is forbidden in Nish (it aborts rather than unwinding): return a `Result<T, E>` for a failure a caller should handle, or `panic(message)` to end the process (reject_throw)
  • debugger: `debugger` is forbidden in Nish (no debugger hook) (reject_debugger)
  • a label on another statement: Labeled statements are forbidden in Nish except on a loop, a block or a `switch` (reject_label_if)
  • for...in: `for...in` is forbidden in Nish (it enumerates property names, and object layout is fixed at compile time); use `for...of` (reject_for_in, reject_map_iter_for_in)

Expressions

Construct Message Test
eval(...), eval as a value `eval` is forbidden in Nish (no interpreter at runtime) reject_eval
Function(...) `Function` constructor is forbidden in Nish (no interpreter at runtime) (validator; no case)
new Function(...) `new Function` is forbidden in Nish (no interpreter at runtime) reject_new_function
Function as a value `Function` is forbidden in Nish (no interpreter at runtime) (validator; no case)
new Proxy(...) `new Proxy` is forbidden in Nish (no dynamic property interception) reject_proxy
Proxy as a value `Proxy` is forbidden in Nish (no dynamic property interception) (validator; no case)
Reflect `Reflect` is forbidden in Nish (no runtime reflection) reject_reflect
Symbol(...), Symbol as a value `Symbol` is forbidden in Nish (no symbol type) reject_symbol_value
globalThis `globalThis` is forbidden in Nish (no global object) reject_globalthis
arguments `arguments` is forbidden in Nish (functions have fixed arity) reject_arguments
undefined as a value, except compared with a Map.get result (Map and Set) `undefined` is forbidden in Nish; use `null` with a `T | null` type reject_undefined_value, reject_map_get_undefined_compare
void expr `void` expressions are forbidden in Nish (no `undefined` value) reject_void_expression
==, != Loose equality is forbidden; use === / !== reject_loose_equality, reject_loose_inequality
in `in` operator is forbidden in Nish (no dynamic property lookup) reject_in_operator, reject_in_newline
a?.[i] inside a chain (the compatibility dialect admits it) Optional chaining `?.` is forbidden in Nish (narrow with `!== null` instead) reject_optional_chain_element_inner
a?.[i] ending a chain, f?.() (in both dialects) An element read cannot end a `?.` chain or be what a `?.` tests: past the end of the array JavaScript answers `undefined` and carries on, and the bounds check panics (test the index against `length`, or narrow the array with `!== null` and read the element into a `const`); An optional call `f?.()` is not supported, with or without `--compat`: call `f()` directly, or narrow `f` with `!== null` first reject_optional_chain_element, reject_optional_call
a?.b outside a chain that is the left operand of ?? (Nullable types; the compatibility dialect admits a chain that is a statement) `?.` is allowed only in a chain that ends in `??`, whose default stands in when a link is `null`: write `a?.b ?? d`, or narrow with `!== null` reject_optional_chain, reject_optional_chain_paren, reject_optional_chain_argument
a ?? b, unless a is a T | null (Nullable types) or a Map.get result (Map and Set); the compatibility dialect admits it Nullish coalescing `??` is forbidden in Nish (narrow with `!== null` instead) reject_nullish
instanceof `instanceof` is forbidden in Nish (no prototype chain) reject_instanceof
comma expressions Comma expressions are forbidden in Nish (write separate statements) reject_comma_expression
typeof x `typeof` is forbidden in Nish (no runtime type tags) reject_typeof_operator
delete x.y `delete` is forbidden in Nish (object layout is fixed) reject_delete
yield `yield` is forbidden in Nish (no coroutine runtime) reject_yield
regex literals Regular expression literals are forbidden in Nish (no regex engine in the runtime) reject_regex, reject_regex_class
bigint literals (10n) `bigint` literals are forbidden in Nish (use number, i32, or f64) reject_bigint_literal
{ __proto__: x } `__proto__` is forbidden in Nish (no prototype chain) reject_proto_literal
x.__proto__ `__proto__` access is forbidden in Nish (no prototype chain) reject_proto_access
x.prototype `.prototype` access is forbidden in Nish (no prototype chain) reject_prototype_access
Object.assign/create/defineProperty/defineProperties/setPrototypeOf/getPrototypeOf `Object.assign` is forbidden in Nish (object layout is fixed at compile time) (and analogous) reject_object_assign, reject_object_define_property, reject_object_set_prototype
o["x"], o[`x`] String-keyed element access is forbidden in Nish; use `obj.name` (no dynamic property lookup) reject_string_key_access
o[true], o[{}], ... (non-numeric-shaped key) Element access requires a numeric index in Nish (no dynamic property lookup) reject_non_numeric_index
dynamic import(...) Dynamic `import()` is forbidden in Nish (modules are resolved at compile time) reject_dynamic_import
import.meta, and a member access or a call through it (import.foo stays a syntax error, and meta is a name everywhere else: tests/parser/names-meta.ts) `import.meta` is forbidden in Nish (a module has no runtime object; resolve paths at compile time) reject_import_meta, reject_import_meta_call

"Numeric-shaped" index keys pass the validator (identifiers, numeric literals, parentheses, unary +/-, + - * / % over those, calls, property and element access); the checker then requires a numeric type.

Each of these is parsed and refused by its rule, once: a module with several reports the first in source order (reject_expr_forms_together). typeof, void, delete, await, yield, in, instanceof, as and satisfies are not reserved, and a variable, a parameter, a field or a function may be called one (tests/parser/names-operators.ts, names-operator-calls.ts). A word is the operator only where the name could not stand:

  • typeof, void, delete, await and yield when an operand follows on the same line — a name, a literal, this, super, new, ! or ~. After a line break, or before (, [, +, -, ++, --, / or <, the word is a name as it always was: typeof(x) and void (0) call a function of that name, typeof -x subtracts, and each is refused as the name it is when nothing declares it, where TypeScript reads the operator.
  • in and instanceof after an operand on its line, or at the start of the next line when an operand follows them there (reject_in_newline). in in a for head's initialiser is the loop's, except inside a bracket that ECMAScript's grammar reads it in again — a parenthesis, an array or object literal, an argument list, an element access, a template substitution, a block, or the branch between a conditional's ? and : (reject_in_for_head_array, _call, _arrow, _element, _template, _object, _block, _conditional). TypeScript's parser differs on the array literal, which it reports as a syntax error; Node follows the grammar.
  • as and satisfies after an operand on its line, which is TypeScript's own rule for them.

A regular expression literal is read where an operand is due, which is where TypeScript reads one; anywhere else a / divides.