Skip to content

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

Open

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

Activity

  1. mohityadav8 commented on Feb 12, 2025

    @mohityadav8

    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

  2. lubieowoce commented on Feb 12, 2025

    @lubieowoce

    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

  3. RyanCavanaugh commented on Feb 12, 2025

    @RyanCavanaugh
    Member

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

  4. Andarist commented on Feb 13, 2025

    @Andarist
    Contributor

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

  5. mohityadav8 commented on Feb 13, 2025

    @mohityadav8

    wht u mean to say i am wrong at that point or what Mateusz Burzyński (@Andarist)

  6. Andarist commented on Feb 13, 2025

    @Andarist
    Contributor

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

  7. Andarist commented on Feb 25, 2025

    @Andarist
    Contributor

    Nathan Shively-Sanders (@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)?

  8. sandersn commented on Apr 14, 2025

    @sandersn
    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.

  9. DetachHead commented on Mar 12, 2026

    @DetachHead
    Contributor

    x.prop which (implicitly! bad!) creates the parent x declaration

    why does it do that? is there an option to disable this behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: JavaScriptThe issue relates to JavaScript specificallyHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions