Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
eps1lon opened this issue Feb 12, 2025 · 8 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@eps1lon
Copy link
Contributor

eps1lon commented Feb 12, 2025

πŸ”Ž 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

@mohityadav8
Copy link

assin it to me let me solve this it is beacuse TypeScript treats undefined variables more loosely in JavaScript files unless strict rules are applied.so i can solve this by doing easiest fix is to ensure foo is defined before usage

@lubieowoce
Copy link

lubieowoce commented Feb 12, 2025

as a fun twist, all of these do error:

Object['defineProperty'](blah2, "name", { value: "default" })
Object.defineProperty(blah2, (void 0, "name"), { value: "default" })
Object.defineProperty(true ? blah2 : {}, "name", { value: "default" })

playground

so i'm guessing something about the exact form Object.defineProperty(obj, 'literal', { ... }) hits some special handling and causes the bug.

EDIT: it also fails if the statement is not at the top level of a module

@RyanCavanaugh
Copy link
Member

Yeah, we treat Object.defineProperty(x, "prop") as a synthetic declaration of x.prop which (implicitly! bad!) creates the parent x declaration

@Andarist
Copy link
Contributor

@lubieowoce this is somewhat expected. Only the most straightforward static-ish patterns are supported by those special property assignments.

@mohityadav8
Copy link

wht u mean to say i am wrong at that point or what @Andarist

@Andarist
Copy link
Contributor

Andarist commented Feb 13, 2025

foo gets added to file.jsGlobalAugmentations symbol table and that gets merged with globals. This mechanism was introduced in #26918

lubieowoce added a commit to vercel/next.js that referenced this issue Feb 25, 2025
This PR changes the server action generated code a bit to work around a
typescript bug and remove some false positives we got while
typechecking: microsoft/TypeScript#61165

The trick is that typescript seems to look for *exactly*
`Object.defineProperty(obj, 'literal', { value: ... })`, and changing
any part of the expression bypasses the bug, so we can just use
`Object['defineProperty']` instead.
@Andarist
Copy link
Contributor

@sandersn do u have an opinion on how this should work and how file.jsGlobalAugmentations should be utilized (should they perhaps depend on checkJs or something)?

@sandersn
Copy link
Member

In TS7, implicit namespace creation in JS is completely gone. And I'm not sure if/when Object.defineProperty special handling will be added either. Either way, there should be an error on foo in the original example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

6 participants