question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide better Typescript definition files

See original GitHub issue

Hi Prismic folks 😃

I’m trying to use the nodejs package with its bundled d.ts files. However, types are not properly imported when using the module. It comes from several facts:

  • First, this comes from the way the module is declared, ie. using a namespace. declare namespace Prismic {...} & export = Prismic;. Only what is in the namespace is visible if i’m not mistaken.
  • As a library, the barel file (index.d.ts) should re-export all public APIs. When fetching the api object with Prismic.getApi(...), I cannot write the proper response type since the PrismicApi type is hidden in the namespace.

It could be better to change the namespace to a plain object while re-exporting all sub modules. This way, both the Prismic object and all APIs are visible.

Last but not the least, a little quote from the TS documentation :

Exporting a namespace from your module is an example of adding too many layers of nesting. While namespaces sometime have their uses, they add an extra level of indirection when using modules. This can quickly become a pain point for users, and is usually unnecessary.

What do you guys think ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:19
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
evolkmanncommented, Apr 18, 2020

Unfortunately this is still open. I guess the issue could be resolved for many of us if d.ts/index.d.ts would also export the other parts of the declaration files, for example ResolvedApi.d.ts.

Now with Angular 9, the compiler issues a warning about the deep import paths, for example here: https://github.com/exportarts/ngx-prismic/issues/109.

I wonder if there is even still the need for separate d.ts files since the project is written in typescript anyway (see #69). What do you think?

2reactions
FibHeapcommented, Sep 17, 2018

with typescript > 2.8 this should work

import * as Prismic from 'prismic-javascript'

type ThenArg<T> = T extends Promise<infer U> ? U : T
type PrismicAPIPromise = ReturnType<typeof Prismic.getApi>
type PrismicAPI = ThenArg<PrismicAPIPromise>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Type Declarations - TypeScript
A declaration file provides a way to declare the existence of some types or values ... We'll learn more about how to write...
Read more >
A quick introduction to “Type Declaration” files and adding ...
In this lesson, we are going to take a closer look at type declaration files which are one of the key ingredients of...
Read more >
Generating TypeScript definition files directly from the source
Try npm install @types/pied-piper__compress , this will try to get the types from DefinitelyTyped. · Manually write the type definitions.
Read more >
Generating TypeScript Definition Files from JavaScript
Write your code in JS and apply JSDoc where needed · Use the forked TypeScript npm i -D typescript-temporary-fork-for-jsdoc · Have a tsconfig....
Read more >
How to create your own TypeScript type definition files (.d.ts ...
Identify imports and exports of a given module. · Identify types of the exported function (arguments and return). · Create initial .d.ts file....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found