spencermountain / wtf_wikipedia

a pretty-committed wikipedia markup parser
https://observablehq.com/@spencermountain/wtf_wikipedia
MIT License
770 stars 129 forks source link

Document not exported #492

Closed hulluP closed 10 months ago

hulluP commented 2 years ago

Hi I am trying to consume this plugin from typescript. And I am struggling a little as the return type Document for the function fetch is not exported ! Code example:

async getHello(): Promise<string> {
     const doc = await fetch("Tisch", {
        lang: "de",
      });

      console.log(doc['_title']);
    return doc['_title'];

The above is a workaround that I parse the result myself. it would be nice though to be able to use the Document type. find the above code @ https://github.com/hulluP/parseWiki Could you please add it to the exported declaration

declare namespace wtf { var version : string export { fetch } export { extend } export { extend as plugin } export { version } }

Thanks

spencermountain commented 2 years ago

hey @hulluP sure - can you make a pr? to be honest, a lot of this typescript stuff is over my head. happy to make any suggested changes cheers

spencermountain commented 2 years ago

hi - is the issue using fetch() instead of wtf.fetch()? by my (limited) understanding of ts, the Document seems to be returned by the fetch function lemme know, thanks

wvanderp commented 1 year ago

I can shine some light on this issue.

In typescript, you need to declare return types for functions. The type can be a class, so what I expect that the user wants is to use the document class as a type.

example code

import {fetch, Document} from 'wtf_wikipedia'

async function fetchDocument(name: string): Document | Document[] {
    return await fetch('Manhattan_(New_York)') ?? []
}

This code fails because (also in normal js) the Document class is not exported.

The fix for this is simple to export the Document class in the index.js. And then a small upgrade of the types file.

I will be looking at the issue so expect a pr in a few days