Microsoft/TypeScript

Object.defineProperty(variable) does not error in `.js` files if `variable` does not exist

Open

#61,165 opened on Feb 12, 2025

View on GitHub
 (9 comments) (2 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
BugDomain: JavaScriptHelp Wanted

Description

🔎 Search Terms

defineProperty, cannot find namee

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about defineProperty

⏯ Playground Link

https://www.typescriptlang.org/play/?noImplicitAny=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&noImplicitReturns=false&alwaysStrict=false&declaration=false&target=3&module=1&filetype=js&strictNullChecks=false#code/PTAEAEBcGcFoGMAWBTeBrAUCUBhRBDAOwHNlQAiAGyOPNEgHsKAVATwAdkBleAJwEt2kOv0IsuuBoQBm-WqAZD+U6PSYBbfGjL9IoZL14NeGAPIAjAFapIAOgAmyWYWQAFI516RWACnPVEABoKQnx1ZHJggG8AN3xKAFdkAC5yR2l8BMphYIB3AUh8fxSMymhkAF8ASiA

💻 Code

// javascript.js
// @ts-check
// Does not error unless in `.ts` file.
Object.defineProperty(foo, "name", { value: "default", writable: false });

🙁 Actual behavior

No error in .js files

🙂 Expected behavior

Errors in .js files with TS2304: Cannot find name 'foo'.

Additional information about the issue

Full repro: https://github.com/eps1lon/tsc-object-define-property

Contributor guide