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

Typescript definitions #166

Merged
merged 7 commits into from
Nov 19, 2023
Merged

Conversation

TabithaLarkin
Copy link
Contributor

This PR implements Typescript compilation as discussed in #141. However, I have taken the approach to incrementally enhance the UX for this, rather than do a wholesale refactor of the project, or move to a different build system as discussed in the comments.

A number of packages did have to be updated in order to enable this, but there is no change to any actual code. More information about what packages have been updated and why can be found on the first commit message. The tsconfig option allowJs has been used to generate types from the javascript files, rather than convereting all files, and associated code to Typescript. This allows for package consumers to use the type definitions from the package, published through the types property that was added into the release.js script.

This all results in the following defintion file being generated

declare function bind(element?: HTMLInputElement | HTMLTextAreaElement, options?: DefaultOptions, debug?: boolean): void;
declare function unbind(element: HTMLInputElement | HTMLTextAreaElement, debug?: boolean): void;
declare function isRomaji(input?: string, allowed?: RegExp): boolean;
declare function isJapanese(input?: string, allowed?: RegExp): boolean;
declare function isKana(input?: string): boolean;
declare function isHiragana(input?: string): boolean;
declare function isKatakana(input?: string): boolean;
declare function isMixed(input?: string, options?: {
    passKanji: boolean;
}): boolean;
declare function isKanji(input?: string): boolean;
declare function toRomaji(input?: string, options?: DefaultOptions, map?: {
    [index: string]: string;
}): string;
declare function toKana(input?: string, options?: DefaultOptions, map?: {
    [index: string]: string;
}): string;
declare function toHiragana(input?: string, options?: DefaultOptions): string;
declare function toKatakana(input?: string, options?: DefaultOptions): string;
declare function stripOkurigana(input?: string, { leading, matchKanji }?: {
    leading?: boolean;
    matchKanji?: string;
}): string;
declare function tokenize(input: string, { compact, detailed }?: {
    compact?: boolean;
    detailed?: boolean;
}): string[] | {
    type: string;
    value: string;
}[];
declare const VERSION = "5.2.0";
declare const TO_KANA_METHODS: {
    HIRAGANA: "toHiragana";
    KATAKANA: "toKatakana";
};
declare const ROMANIZATIONS: {
    HEPBURN: "hepburn";
};
type DefaultOptions = {
    useObsoleteKana?: boolean;
    passRomaji?: boolean;
    convertLongVowelMark?: boolean;
    upcaseKatakana?: boolean;
    IMEMode?: boolean | "toHiragana" | "toKatakana";
    romanization?: "hepburn";
    customKanaMapping?: {
        [index: string]: string;
    };
    customRomajiMapping?: {
        [index: string]: string;
    };
};
export { bind, unbind, isRomaji, isJapanese, isKana, isHiragana, isKatakana, isMixed, isKanji, toRomaji, toKana, toHiragana, toKatakana, stripOkurigana, tokenize, VERSION, TO_KANA_METHODS, ROMANIZATIONS };
//# sourceMappingURL=index.d.ts.map

I'm not sure how you would like to proceed with testing this, and would be open to any guidance or experience anyone has in this area.

…compiler for type definitions

As part of adding Typescript, a number of dependencies had to be updated. All @babel packages and
@rollup packages were updated. Additionally prettier-eslint-cli had to be updated due to a typing
issue in the dependency tree. This also meant that eslint also had to be updated to maintain
compatibility. Due to these package changes the minumum node devEngines version has been bumped to
16. A tsconfig.json file has been added, as well as renaming the base index file from index.js to
index.ts.

Adds packages to enable WaniKani#141
…e definition output

constants.js has been changed to a typescript file to enable the exporting of the DefaultOptions
type. This is then used by various function definitions, which provides type checking for the
parameters. Regexp has been changed to RegExp as Regexp was resolving to the any type. Other
parameters defined as Object have been specified with types to match their expected input.
TOKEN_TYPES's export was removed as it was unused and was being generated into the d.ts file.

Resolve external typings for WaniKani#141
@DJTB
Copy link
Collaborator

DJTB commented Oct 22, 2023

This is a great addition, thanks for contributing!
It's probably easiest to test this manually by creating a release locally and importing to another project.
I'll give this a go over the next week or so, and check the docs site compiles correctly as well.

@TabithaLarkin
Copy link
Contributor Author

Just a heads up, I've now tested this locally, by adjusting the package.json to point at the types file and dropping the types file into the correct folder in a local install. It seems to work as expected!

image

@DJTB
Copy link
Collaborator

DJTB commented Oct 27, 2023

Sorry for the delay, I've taken a quick look and the docs site no longer compiles due to invalid syntax.
E.g. 3df06ab#diff-cc3afe3f35bff79cef9a329c0b73c22fbcd68698520d712457c1a271332e3bf9R14

You can try running npm run docs to build and npm run docs:view to view locally.

Would you mind taking a look at this?

@TabithaLarkin
Copy link
Contributor Author

Sure thing. I'll take a look at it this weekend.

…les and typings

Added better-docs to piggyback off of its typescript plugin. Updated instances of string to String
and boolean to Boolean. This matches the rest of the existing documentation. Typescript types are
unaffected by these changes and automatically get converted to the correct typescript type. Changed
instances of [index: string]: string to Object.<string, string> JSDoc type. This gets converted in
typescript, but the previous syntax was not supported in JSDocs.

WaniKani#141
@TabithaLarkin
Copy link
Contributor Author

Hey sorry for the delay. Things have been pretty hectic on my end.

I've updated the doc generation to handle typescript files, and changed any conflicting types. Generated typescript types are not affected by this change. I've also changed some casing of typings back, such as string to String in order to match the previous documentation. It should be good to go now!

@DJTB
Copy link
Collaborator

DJTB commented Nov 13, 2023

Hey sorry for the delay. Things have been pretty hectic on my end.

I've updated the doc generation to handle typescript files, and changed any conflicting types. Generated typescript types are not affected by this change. I've also changed some casing of typings back, such as string to String in order to match the previous documentation. It should be good to go now!

No problem, busy here as well, but I'll do my best to take another look at it this week.

Copy link
Collaborator

@DJTB DJTB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some minor changes, but otherwise this is looking good now 👍
I'll publish on the weekend for you.

@DJTB DJTB merged commit 0e79adb into WaniKani:dev Nov 19, 2023
@DJTB DJTB mentioned this pull request Nov 19, 2023
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