I am curious about .d.ts declaration files because I am new to the TypeScript programming language. I was told by someone that .d.ts files are are similar to .h header files in the C & C++ programming languages, however, the .d.ts files don't seem to work quite the same. Currently, I am failing to understand how to properly use the .d.ts files. It would appear that I cant add my .js or .ts files to the .d.ts files, so the only way my project will work is if it contains all three file types. That seems like a lot of files. To help me better understand how the .d.ts files are related to JavaScript & TypeScript, I have some questions I would like to ask.
这三个文件之间的关系是什么?他们之间的关系?
如何使用*.d。ts文件?这是否意味着我可以删除*。Ts文件永久?
如果是这样,*.d。ts文件知道哪个JS文件映射到自己?
如果有人能给我举个例子就太好了。
“d.ts”文件用于提供关于用JavaScript编写的API的typescript类型信息。这个想法是你使用jQuery或下划线之类的东西,一个现有的javascript库。你想从你的typescript代码中使用它们。
而不是重写jquery或下划线或typescript中的任何东西,你可以写d.ts文件,它只包含类型注释。然后,从你的typescript代码中,你可以获得静态类型检查的typescript好处,同时仍然使用纯JS库。
这多亏了TypeScript的约束,不允许你添加“。导入语句末尾的Ts扩展名。正因为如此,当你引用某个文件时,比如说my-module。js,如果有一个my-module。d。ts,然后TypeScript会包含它的内容:
src/
my-module.js
my-module.d.ts
index.ts
my-module.js
const thing = 42;
module.exports = { thing };
my-module.d.ts
export declare const thing: number;
index.ts
import { thing } from "./my-module"; // <- no extension
// runtime implementation of `thing` is taken from ".js"
console.log(thing); // 42
// type declaration of `thing` is taken from ".d.ts"
type TypeOfThing = typeof thing; // number
因为源代码是真相的最终来源。这里似乎是它的实现:
/*
* Every module resolution kind can has its specific understanding how to load module from a specific path on disk
* I.e. for path '/a/b/c':
* - Node loader will first to try to check if '/a/b/c' points to a file with some supported extension and if this fails
* it will try to load module from directory: directory '/a/b/c' should exist and it should have either 'package.json' with
* 'typings' entry or file 'index' with some supported extension
* - Classic loader will only try to interpret '/a/b/c' as file.
*/
type ResolutionKindSpecificLoader = (extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState) => Resolved | undefined;
and
/**
* Kinds of file that we are currently looking for.
*/
const enum Extensions {
TypeScript = 1 << 0, // '.ts', '.tsx', '.mts', '.cts'
JavaScript = 1 << 1, // '.js', '.jsx', '.mjs', '.cjs'
Declaration = 1 << 2, // '.d.ts', etc.
Json = 1 << 3, // '.json'
ImplementationFiles = TypeScript | JavaScript,
}
完整的文件是https://github.com/microsoft/TypeScript/blob/main/src/compiler/moduleNameResolver.ts。
“d.ts”文件用于提供关于用JavaScript编写的API的typescript类型信息。这个想法是你使用jQuery或下划线之类的东西,一个现有的javascript库。你想从你的typescript代码中使用它们。
而不是重写jquery或下划线或typescript中的任何东西,你可以写d.ts文件,它只包含类型注释。然后,从你的typescript代码中,你可以获得静态类型检查的typescript好处,同时仍然使用纯JS库。
这多亏了TypeScript的约束,不允许你添加“。导入语句末尾的Ts扩展名。正因为如此,当你引用某个文件时,比如说my-module。js,如果有一个my-module。d。ts,然后TypeScript会包含它的内容:
src/
my-module.js
my-module.d.ts
index.ts
my-module.js
const thing = 42;
module.exports = { thing };
my-module.d.ts
export declare const thing: number;
index.ts
import { thing } from "./my-module"; // <- no extension
// runtime implementation of `thing` is taken from ".js"
console.log(thing); // 42
// type declaration of `thing` is taken from ".d.ts"
type TypeOfThing = typeof thing; // number