tests-always-included/password-strength

Could not find a declaration file for module 'tai-password-strength'

Closed this issue · 10 comments

Hello.

I'm using this library in the browser. I installed it with npm i tai-password-strength, and I'm getting this warning:

Could not find a declaration file for module 'tai-password-strength'. 'path/to/project/node_modules/tai-password-strength/lib/node.js' implicitly has an 'any' type. Try npm install @types/tai-password-strength if it exists or add a new declaration (.d.ts) file containing declare module 'tai-password-strength';

Everything works fine, but I don't like having the warning... could you take a look at it?

Do you use TypeScript? If so, we could use a contribution to add typings to the JavaScript library.

Well I use TypeScript but I've never generated a typings file or uploaded any library to npm.

This page describes the process, it does not seem very complicated:

https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html

Are you trying this or do you want me to do a pull request?

All right. I cloned this repository and tried the steps in the link. It failed, showing this error: lib/password-strength.js:6:1 - error TS9005: Declaration emit for this file requires using private name 'PasswordStrength'. An explicit type annotation may unblock declaration emit.

It seems a bug in typescript: microsoft/TypeScript#37832

Apparently it will be fixed in the next version. I'll try again in a couple of weeks.

I had a little time to make this version of index.d.ts, but I don't know if it will work. Would you have time to test it out? If it works, or if it provides a base that would work, it would be stellar if you would include it in a pull request.

export interface PassswordStrengthExports {
    PasswordStrength: PasswordStrength;
    commonPasswords: string[];
    trigraphs: PasswordStrengthTrigraphMap;
}

export class PasswordStrength {
    charsets: PasswordStrengthGroups;
    commonPasswords: null | string[];
    trigraph: null | PasswordStrengthTrigraphMap;

    addCommonPasswords(passwords: string[] | string): this;
    addTrigraphMap(map: PasswordStrengthTrigraphMap): this;
    charsetGroups(password: string): { [key: string]: boolean | string };
    charsetSize(groups: PasswordStrengthGroups): number;
    check(password: string): PasswordStrengthStatistics;
    checkCommonPasswords(password: string): boolean;
    checkTrigraph(password: string, charsetSize: number): number;
    determineStrength(status: PasswordStrengthStatistics): PasswordStrengthCode;
    nistScore(password: string): number;
    otherChars(password: string): string;
    shannonScore(password: string): number;
}

export interface PasswordStrengthGroups {
    [key: string]: string;
}

export interface PasswordStrengthTrigraphMap {
    [key: string]: number;
}

export interface PasswordStrengthStatistics {
    charsetSize: number;
    commonPassword: boolean;
    passwordLength: number;
    shannonEntropyBits: number;
    strength: PasswordStrengthCode;
    trigraphEntropyBits: null | number;
}

export enum PasswordStrengthCode {
    VERY_WEAK = "VERY_WEAK",
    WEAK = "WEAK",
    REASONABLE = "REASONABLE",
    STRONG = "STRONG",
    VERY_STRONG = "VERY_STRONG"
}

It worked! I just had to paste that code into lib/node.d.ts (not index.d.ts)

Published a new version with updated dependencies and the TypeScript file. Would you please confirm it worked?

I just tested it. It works. Thank you!

Thanks for the help!