“This is the second day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

What is a declaration file

Declaration files are typescript concepts that describe variable types,

The declaration file does not contain an implementation; it is just a type declaration

Who will the declaration file serve?

Declare file to describe variable types in JS file for TS

Does autoJS require a declaration file

Vscode will use the contents of the declaration file as a code hint, so you don’t have to look up the documentation, which is one of the benefits;

The previous code hint was implemented through code snippets,

The plugins now remove the snippet files and replace them with.d.ts files

The path to vscode’s autojs plug-in file

C: \ Users \ Administrator vscode \ extensions \ hyb1996 auto – js – pro – ext – 1.3.2

Where do I get the autoJS declaration file

Open vscode and follow these steps:

  • According to the formula one
  • Input autojsv8
  • Js Pro: New V8 API Projcet
  • Select the folder where the project files will be placed
  • The declaration file appears
  • node_modules@autojs\types-pro8\index.d.ts
  • You need a tsconfig.json file to specify where the declaration file is, with the field: typeRoots

Declare file index.d.ts

D. ts filename is TypeScript Declaration File

Take a look at the file to see what declaration formats are available:

  • declare module ‘app‘ { … }
  • declare var app: AutoJs.App;
  • declare function sleep(n: number);
  • declare namespace AutoJs { … }

And almost always global, to indicate that this is a global declaration.

So let’s just go ahead and write our globally-declared variable in vscode, mouse over it, and we’ll display the code hint,

You don’t have to check the documents, save 10 seconds at a time, and see how much time you can save.

From top to bottom

  • The statement module
  • Declare a variable
  • Declare functions
  • Declaring a namespace

Module and Namespace seem to be the same, so I checked and the official document explains as follows:

A note about terminology: It’s important to note that in TypeScript 1.5, “Internal modules” are now “namespaces”. “External modules” are now “modules”. As to align with ECMAScript 2015’s Terminology, (namely that module X { is equivalent to the now-preferred namespace X {).

Module X is the same as Namespace X. Please correct any mistakes.

How do YOU declare a function

Let’s see how images. Save is declared.

images.save(image, path[, format = “png”, quality = 100])

First of all, let’s disassemble the function

  • He belongs to the object images
  • His name is Save
  • It has four parameters, two required parameters and two default parameters

And then in vscode we say images.save,

Hover your mouse over images to see the following code prompt

Hover over Save to see the following code prompt

Hold Down CTRL and click images to see the declaration

declare var images: AutoJs.Images;
Copy the code

Hold Down CTRL and click Save to see the declaration, which is all the code prompts in the image above

/** * Save image as PNG in path. If the file does not exist, it will be created. File existence will be overwritten. Var img = images.read("/sdcard/1.png"); * images.save(img, "/sdcard/1.jpg", "jpg", 50); * app.viewFile("/sdcard/1.jpg"); * @param image image * @param path Path * @param format Image format. ** 'PNG' ** 'jpeg'/' JPG '**' webp '* @param Quality Image quality, 0 to 100 integer values **/ save(image: image, path: string, format? : "png" | "jpg" | "jpeg" | "webp", quality? : number);Copy the code

Among the four save parameters, the parameter types of path, format and quality are all basic types. String and number are optional parameters. : said,

The hierarchy of the save method

declare module '__images__' { global { namespace AutoJs { interface Images { save( ... ) ;Copy the code

The first parameter is Image, so let’s hold Ctrl, Image and see where we go?

Image

class Image { getWidth(): number; readonly width: number; getHeight(): number; readonly height: number; saveTo(path: string); pixel(x: number, y: number): number; recycle(); isRecycled(): boolean; readonly mat? : Mat; readonly bitmap? : any; }Copy the code

Image level

declare module '__images__' {
  global {
    namespace AutoJs {
      class Image {
Copy the code

The declaration of the image color is placed in the image color module, and then declared as global, belonging to the named control AutoJs

The Image class is written specifically for the first argument to the save method,

The parameter types of save are basically basic except Mat. Click Mat to see where it is declared.

type Mat = any;
Copy the code

Any, terminator of variable type.

Type is used to declare a variable type Mat, whose real type is any.

Some declaration examples

declare var name: string; declare var age: number; declare var id: number; declare const SMALL_TARGET: 1_0000_0000; declare function getName(): string; declare function getName(id: string): string; declare function isSingleDog(name? : string): boolean;Copy the code

The statement module

foo.d.ts

export let name: string;
export function getNameURL2(num888888: string): string;
namespace student {
  export let name: string;
  export let age: number;
  export function doWhat(what: string): string;
}
Copy the code

foo.js

let name = "yashu666"; function getNameURL2(num) { return "numberURL2 is " + num; } let student = { name: "lucy", age: "16", doWhat: function (info) { console.log("student is doing " + info); }}; module.exports = { name, getNameURL2, student };Copy the code

main.ts

import URL2 from "./foo/foo";
console.log(URL2);
console.log(URL2.name);
URL2.getNameURL2;
URL2.student.doWhat("homework");
Copy the code

Pay attention to

  • Foo. d.ts and foo.js should be in the same folder
  • The code in main.ts prompts for the content in foo.d.ts
  • Main.ts runs the content in foo.js

Quotes.

Ideas are the most important, other Baidu, Bing, StackOverflow, Github, Android docs, AutoJS docs, and last but not least, ask in the group

The statement

This tutorial is intended for learning purposes only and is not intended for any other use

Wechat official account tooth Uncle tutorial