Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Runtypes "Object" breaks decorators #487

@joeyparrish

Description

@joeyparrish

Importing "Object" from runtypes while also using ES decorators causes runtime failures like:

 Exception during run: TypeError: Object.getOwnPropertyDescriptor is not a function

To make it work, I find I have to rename Object on import:

import {
  // Work around
  Object as Dictionary,
  type Parsed,
  String,
} from 'runtypes';

const LoginRequest = Dictionary({
  username: String,
  password: String,
});

export class TestController extends ControllerBase {
  @Post('/login')
  static login(req: Request): Response {
    return req.password == 'foo';
  }
}

The Post decorator (source not shown) doesn't use Object directly. But TypeScript emits a helper function called __esDecorate that uses Object.getOwnPropertyDescriptor internally. But the runtype Object import masks the global Object.

This isn't an issue if I use decorators and runtypes in separate modules, but I'm not far enough into the project to know if I would still have this issue if I start bundling everything into a single file.

Arguably, TypeScript should generate code that references globalThis.Object.getOwnPropertyDescriptor instead. In the mean time, maybe this should get a mention in the README, so people know they may need to rename Object to avoid clobbering the global one?

If I can make a minimal repro for a TypeScript bug report, I'll link to it here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions