Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect type-declaration #77

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Fix incorrect type-declaration #77

wants to merge 3 commits into from

Conversation

manuth
Copy link

@manuth manuth commented Oct 5, 2020

General

The front-matter module uses the module.exports = fm-pattern to expose the library.
Currently the type-declaration doesn't represent this correctly and instead asserts an export default fm.

This contradicts with each other and causes typescript to not work correctly.

This PR fixes the type-declarations accordingly.

The Solution

The solution is to move every existing component (such as FrontMatterOptions, FM etc.) into a namespace which merges with the fm-function:

front-matter/index.d.ts

Lines 1 to 23 in d367565

declare namespace fm {
export interface FrontMatterResult<T> {
readonly attributes: T
readonly body: string
readonly bodyBegin: number;
readonly frontmatter?: string
}
export interface FrontMatterOptions {
/**
* Whether to use [safeload](https://github.com/nodeca/js-yaml#safeload-string---options-)
* @default true
*/
allowUnsafe?: boolean
}
export interface FrontMatter {
<T>(file: string, options?: FrontMatterOptions): FrontMatterResult<T>
test(file: string): boolean
}
}
declare const fm: fm.FrontMatter;

Afterwards the fm-function can be exported the same way it is exported in the actual source-file (which is using an export = fm-expression):

export = fm;

Merging this PR fixes issue #76

@karlhorky
Copy link

@jxson @peterbe @tyankatsu0105 what do you think about this PR?

It looks like the current TypeScript types are broken:

Screenshot 2024-07-11 at 10 09 30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants