简体   繁体   English

TypeScript再次结合* .js + * .d.ts?

[英]TypeScript combining *.js + *.d.ts again?

TypeScript tsc -d "declaration" of "compilerOptions" generates corresponding '.d.ts' file. TypeType tsc -d “ compilerOptions”的“声明”生成相应的'.d.ts'文件。

For instance, from: 例如,来自:

tmp.ts tmp.ts

const log = (m: unknown) => {
    console.log((m)); 
    return m;
};

it generates: 它产生:

tmp.js tmp.js

const log = (m) => {
    console.log((m));
    return m;
};

and: 和:

tmp.d.ts tmp.d.ts

declare const log: (m: unknown) => unknown;

I think this is quite interesting because it "devides" the TypeScript source code to a native JavaScript code and the extra type definition. 我认为这很有趣,因为它可以将TypeScript源代码“划分”为本地JavaScript代码和其他类型定义。

Then, here is my thought. 然后,这就是我的想法。 After deviding the native code and type definition, is it easily possilbe to generate a valid TypeScript code by re-binding both codes. 在指定了本机代码和类型定义之后,可以很容易地通过重新绑定两个代码来生成有效的TypeScript代码。

For instance: 例如:

tmp-reunion.ts tmp-reunion.ts

declare const log1: (m: unknown) => unknown;

const log1 = m => {
    console.log((m)); 
    return m;
};

This code generates an errors: 此代码生成错误:

[ts] Cannot redeclare block-scoped variable 'log1'.

for each statements. 对于每个语句。

Why am I doing this? 我为什么要这样做?

I'm motivated by facts: 我受到事实的激励:

  1. In Algebra, 在代数中

    (x^2 + 3x +2) = (x+1)(x+2) = (x^2 + 3x +2) (x ^ 2 + 3x +2)=(x + 1)(x + 2)=(x ^ 2 + 3x +2)

I want to confirm if the same thing is valid under TypeScript. 我想确认在TypeScript下是否同样有效。

  1. It's especially interesting TypeScript compiles a typed-source-code to a vanilla JavaScript code having the type information completely discarded as the output. 尤其有趣的是TypeScript将类型化的源代码编译为原始的JavaScript代码,而该类型的信息已被完全丢弃为输出。

The fact makes me think, in a sense, the output of the vanilla JS code is type safe and valid with TypeScirpt, just without the *.d.ts file which is eventually appearently merely an extra helper functionality that TypeScript compiler takes advantage of. 从某种意义上说,事实使我认为,使用TypeScirpt时,纯净的JS代码的输出是安全且有效的类型,只是没有* .d.ts文件,该文件最终似乎只是TypeScript编译器利用的额外辅助功能。 It's just a tool. 这只是一个工具。

Accordingly, the type-safty of the vanilla JS code can be later, easily validated by TypeScript compiler with the helper functional tool, that is tmp.d.ts . 因此,以后可以通过TypeScript编译器使用辅助功能工具tmp.d.ts轻松验证香草JS代码的类型安全性。 That is what I want to confirm. 我要确认一下。

How can I make TypeScript combining *.js + *.d.ts again? 如何使TypeScript再次结合* .js + * .d.ts?

Thanks. 谢谢。

Accordingly, the type-safty of the vanilla JS code can be later, easily validated by TypeScript compiler with the helper functional tool, that is tmp.d.ts . 因此,以后可以通过TypeScript编译器使用辅助功能工具tmp.d.ts轻松验证香草JS代码的类型安全性。

This is not true in general because you've lost any manual type annotations inside function bodies that were needed to validate those function bodies where TypeScript's type inference was insufficiently powerful. 通常这是不正确的,因为您已经丢失了用于验证TypeScript类型推断功能不够强大的那些函数体的函数体内的任何手动类型注释。 For an artificial example (assuming noImplicitAny of course): 举一个人工的例子(当然假设noImplicitAny ):

function duplicateEach<T>(arr: T[]) { 
    let out: T[] = [];
    arr.forEach(t => { out.push(t); out.push(t); });
    return out;
}

If you remove the annotation on out , the code doesn't compile because TypeScript's evolving array type inference doesn't traverse into callbacks. 如果在out上删除注释,则由于TypeScript不断发展的数组类型推断不会遍历回调,因此代码不会编译。 If we look at the .d.ts and the .js , this annotation doesn't appear in either of them: 如果我们查看.d.ts.js ,则此注释不会出现在其中任何一个中:

declare function duplicateEach<T>(arr: T[]): T[];

function duplicateEach(arr) {
    var out = [];
    arr.forEach(function (t) { out.push(t); out.push(t); });
    return out;
}

For more realistic examples, look at any nontrivial TypeScript codebase. 有关更实际的示例,请查看任何平凡的TypeScript代码库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM