L
L
lexstile2020-04-15 12:42:11
typescript
lexstile, 2020-04-15 12:42:11

How to add new type for function from node_modules?

There is a react-input-mask module .
It has a beforeMaskedStateChange function, for which types have not yet been described.
Type code from node_modules:

import * as React from 'react';

export interface Selection {
  start: number;
  end: number;
}

export interface InputState {
  value: string;
  selection: Selection | null;
}

export interface MaskOptions {
  mask: string | Array<(string | RegExp)>;
  maskChar: string;
  alwaysShowMask: boolean;
  formatChars: Record<string, string>;
  permanents: number[];
}

export interface Props extends React.InputHTMLAttributes<HTMLInputElement> {
  mask: string | Array<(string | RegExp)>;
  maskChar?: string | null;
  formatChars?: { [key: string]: string };
  alwaysShowMask?: boolean;
  inputRef?: React.Ref<HTMLInputElement>;

  beforeMaskedValueChange?(
    newState: InputState,
    oldState: InputState,
    userInput: string,
    maskOptions: MaskOptions,
  ): InputState;
}

export class ReactInputMask extends React.Component<Props> {
}

export default ReactInputMask;

I need to add types for a function similar to beforeMaskedValueChange - beforeMaskedStateChange .
I create a @types folder in src, then a folder with the name of the module and an index file, in it (this is how it works):
declare module 'react-input-mask' {
    import * as React from 'react';

    export interface Selection {
        start: number;
        end: number;
    }

    export interface InputState {
        value: string;
        selection: Selection | null;
    }

    export interface Props extends React.InputHTMLAttributes<HTMLInputElement> {
        mask: string | (string | RegExp)[];
        maskChar?: string | null;
        formatChars?: { [key: string]: string };
        alwaysShowMask?: boolean;
        inputRef?: React.Ref<HTMLInputElement>;
        beforeMaskedStateChange?({
            nextState: InputState,
            previousState: InputState,
            currentState: InputState,
        }): InputState;
    }
    export class ReactInputMask extends React.Component<Props> {}
    export default ReactInputMask;
}

But how to make it possible to export only one function in the latter - beforeMaskedStateChange?
So that he merges my types and from node_modules.
PS Do not confuse beforeMaskedStateChange with beforeMaskedValueChange.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question