0

i am working on an Asp.net MVC project with Smartadmin theme and Typescript with JQuery typings for Dom manipulation, there is a jquery library called SmartMessageBox for prompt messages ,is there an easy way to use this library inside my typescript file because i get Error Build:Property 'SmartMessageBox' does not exist on type 'JQueryStatic'

  • You can write a declaration file describing the library. If you post a link the project documentation maybe I could help, but I can't find the library on a simple search – Titian Cernicova-Dragomir Feb 05 '18 at 09:37
  • you need to have type definitions for that plugin or you can cast `jQuery` to `any` before using the `SmartMessageBox` property, eg. `(jQuery as any).SmartMessageBox(args)` – roomcayz Feb 05 '18 at 09:37
  • 1
    @Roomy that is a possibility, but not a very safe one, if the library is simple, it might pay off to write the declarations . Maybe even contribute them to definitely typed ;) – Titian Cernicova-Dragomir Feb 05 '18 at 09:38
  • unfortunately it's library written by the theme creator i think,i couldn't find it anywhere – Mohammed Hady Feb 05 '18 at 10:25

2 Answers2

1

If the message box is only used in one file, the easiest solution is to add something like this at the top of the file:

declare const jQuery: JQueryStatic & {
    // *** Choose one of the following ***
    // Most basic:
    SmartMessageBox: any;
    // Better (assuming SmartMessageBox is a function):
    SmartMessageBox(/*param type info*/): /*return type or void*/;
};

This says jQuery is a global object whose type is the union of the interface JQueryStatic and the interface describing SmartMessageBox.

If the message box or other "smart admin" types are used in multiple files, you should look into writing a .d.ts file. The official documentation and the default jQuery .d.ts file should be helpful in figuring out what to do. Save the file under <project root>/typings/smart-admin/index.d.ts and add the following in your tsconfig.json:

{
    "compilerOptions": {
        "typeRoots": ["./node_modules/@types", "./typings"]
    }
}

This tells the compiler to look for type definitions under ./typings as well as ./node_modules/@types (the default location for @types packages installed from npm).

ecraig12345
  • 2,328
  • 1
  • 19
  • 26
0

You must create a d.ts file for SmartMessageBox. https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html

Akash Singh
  • 125
  • 2
  • 3