Skip to content

Empty object type is not workingΒ #47486

Closed
@Asaf-S

Description

@Asaf-S

Bug Report

πŸ”Ž Search Terms

  • Empty object Typescript

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed all of the FAQ's entries.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const emptyObject: Record<string, never> = {};
console.log(emptyObject.id); // Should warn, but doesn't

const nonEmptyObject: Record<'a', unknown> = { a: 1 };
console.log(nonEmptyObject.id); // Warning: Property 'id' does not exist on type 'Record<"a", unknown>'

πŸ™ Actual behavior

I wrapped the Request type of the Express library, in order to change the default value of the Request.params property.

type IEmptyObject = Record<string, never>;
export interface ICustomRequest<P extends IParams = IEmptyObject> extends Request<P> {}
function API(req: ICustomRequest) {
  // Expected to get a warning, but didn't
  console.log(req.params.anyParam);
}
express.get("/route", API)

If I would give a non-empty type, then it works:

function API(req: ICustomRequest<{definedParam:string}>) {
  // Now I get a warning
  console.log(req.params.anyParam);
}

πŸ™‚ Expected behavior

I should have been warned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions