Skip to content

Create typing definition file or as a separate @types module, for TypeScript  #4

Open

Description

@yuriys-kentico

I have used this library with React and TypeScript and created this typing definition to not have my compiler complain:

import { Component, ComponentLifecycle } from "react";

export declare function enableUniqueIds(component: Component): void;
export declare function resetUniqueIds(): void;

declare module "react" {
    export interface Component<P = {}, S = {}, SS = any> extends ComponentLifecycle<P, S, SS> {
        nextUniqueId(): string;
        lastUniqueId(): string;
        getUniqueId(identifier: string): string;
    }
} 

It is possible another interface would be a better spot for this, however I have used this with React.PureComponent and observed the expected results (i.e. an element had id="id-2-1").

I used it by manually adding the typing definition file to node_modules. Here is a class with this in use TransformedImagesElement/src/components/editor/inputs/SwitchInput.tsx. Manually adding it is not ideal, which leads to two possible solutions:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions