Files
scriptc/tests/diagnostics/utility-types.ts
T
Chris Tate 85fe87ffbf Convert Date values into unknown slots as Dates
A TypeScript Date placed in an `unknown` slot (an annotated binding, an
object or array literal, a record or class field) becomes a Date holding
the same time value. Before, a Date member of an `unknown` object literal
became a function, and other conversions were refused. util.inspect prints
these Dates in their ISO form or as Invalid Date, and their methods can be
read as values through untyped references. Comparisons with a Date operand
keep their fence: each conversion creates a new Date.
2026-10-18 05:40:08 -05:00

24 lines
1.2 KiB
TypeScript

// The fences around utility-type record shapes. Utility types over data
// shapes compile (Partial/Record/Pick/Omit/Readonly resolve to ordinary
// record shapes — differential corpus), and STRING and NUMBER index
// signatures over supported value types compile as hybrid shapes (declared
// fields + an overflow map; number keys canonicalize to their JS string
// spelling; function/Map/Set/nested-record values ride the same store —
// differential corpus); what stays rejected:
// - symbol-keyed index signatures (no lowering for symbol keys);
// - index-signature value types outside the overflow-map slice (Dates and
// other lib API objects, even when the value itself has a representation).
// (Readonly<Date> bindings compile, holding the Date as a checked value.
// Dot access/writes to index-signature keys compile now — the overflow
// path in dot spelling — and self-referential mapped types intern as
// named recursive shapes; differential corpus covers both.)
const bySymbol: { [s: symbol]: string } = {};
console.log(bySymbol);
const dateBag: { [k: string]: Date } = {};
const boot = dateBag["boot"];
console.log(boot);
const stamped: Readonly<Date> = new Date(0);
console.log(stamped.getTime());