typescriptdenoecmascript-temporaltypescript-namespace

How to use Temporal as a type?


When I try:

const a: Temporal = new Temporal.PlainDate(2024, 2, 1)

I get:

Cannot use namespace 'Temporal' as a type.deno-ts(2709)

What happens? This contrasts with how Date is used:

const b: Date = new Date(2024, 2, 1)

Solution

  • Temporal is not a type, but a namespace. From Namespaces and Modules:

    Namespaces are simply named JavaScript objects in the global namespace.

    or from Namespaces:

    “Internal modules” are now “namespaces”. “External modules” are now simply “modules”

    To fix this, you need to use the correct type Temporal.PlainDate:

    const a: Temporal.PlainDate = new Temporal.PlainDate(2024, 2, 1)