Object.defineProperty declarations in JavaScript

When writing in JavaScript files (using allowJs), TypeScript now recognizes declarations that use Object.defineProperty.This means you’ll get better completions, and stronger type-checking when enabling type-checking in JavaScript files (by turning on the checkJs option or adding a // @ts-check comment to the top of your file).

  1. // @ts-check
  2. let obj = {};
  3. Object.defineProperty(obj, "x", { value: "hello", writable: false });
  4. obj.x.toLowercase();
  5. // ~~~~~~~~~~~
  6. // error:
  7. // Property 'toLowercase' does not exist on type 'string'.
  8. // Did you mean 'toLowerCase'?
  9. obj.x = "world";
  10. // ~
  11. // error:
  12. // Cannot assign to 'x' because it is a read-only property.