用 TypeScript 编写闭包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32380345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Coding a closure in TypeScript
提问by MatthewScarpino
Just for amusement, I'm trying to use TypeScript's strong typing in code containing a closure. Here's what the original JavaScript code looks like:
只是为了娱乐,我试图在包含闭包的代码中使用 TypeScript 的强类型。下面是原始 JavaScript 代码的样子:
var func = (function() {
var private_count = 0;
var increment = function() {
private_count += 1;
return private_count;
}
return increment;
}());
Here's my best attempt to implement this with TypeScript:
这是我用 TypeScript 实现这一点的最佳尝试:
var func: ()=>()=>number = (function(): ()=>number {
var _count: number = 0;
var increment: ()=>number = function(): number {
_count += 1;
return _count;
}
return increment;
}());
func is a function that returns a function that returns a number. I've set its type to '()=>()=>number', but the compiler doesn't like that. I know this isn't the most practical use of TypeScript, but does anyone have any thoughts for fixing the compile error?
func 是一个函数,它返回一个返回一个数字的函数。我已将其类型设置为 '()=>()=>number',但编译器不喜欢那样。我知道这不是 TypeScript 最实际的用途,但是有人对修复编译错误有任何想法吗?
回答by David Sherret
You could leave the code as-is. TypeScript already figures out that the types of all the variables in your code by looking at the initial assignment. Hover over the variables and you will see the types it's figured out.
您可以保留代码原样。TypeScript 已经通过查看初始赋值来确定代码中所有变量的类型。将鼠标悬停在变量上,您将看到它计算出的类型。
The main problem in the code is that the type of func
is incorrect. This:
代码中的主要问题是类型func
不正确。这:
var func: () => () => number = ...
...should be this:
……应该是这样的:
var func: () => number = ...
Note there's no need for the extra () =>
because it's not a function that returns a function that returns a number. It's only a function that returns a number.
请注意,不需要额外的,() =>
因为它不是一个返回一个返回数字的函数的函数。它只是一个返回数字的函数。
By the way, if you really want to explicitly type everything, here's another solution:
顺便说一句,如果您真的想明确键入所有内容,这是另一种解决方案:
var func: () => number = (() => {
var private_count: number = 0;
var increment: () => number = () => {
private_count += 1;
return private_count;
};
return increment;
})();
But I would recommend just using implicit types (as long as they're not implicit any
types), but that's just a personal preference:
但我建议只使用隐式类型(只要它们不是隐式any
类型),但这只是个人偏好:
var func = (() => {
var private_count = 0;
var increment = () => {
private_count += 1;
return private_count;
};
return increment;
})();
回答by Laquio
I did try this and it works!
我确实尝试过这个并且有效!
file1.ts
文件1.ts
export var Mvar = (() => {
var private_arry = [];
function pushfn(val):any {
return private_arry.push(val);
}
function countfn():number {
return private_arry.length;
}
function setfn(val):void {
private_arry = null;
private_arry = val;
}
function getfn(val?:number):any {
if(val!==undefined) {
return private_arry[val];
}
return private_arry;
}
return {
push:pushfn,
count:countfn,
set:setfn,
get:getfn
}
})()
file2.ts
文件2.ts
import { Mvar } from '../../shared/file1';
ngOnInit() {
console.log("private var: " + Mvar.push("e1"));
console.log("private count: " + Mvar.count());
console.log("private get: " + Mvar.get());
...
}
回答by thoughtrepo
Here it is:
这里是:
var func = ((): () => number => {
var _count: number = 0;
var increment: () => number = function (): number {
_count += 1;
return _count;
}
return increment;
})();
But adding an interface makes it easier.
但是添加一个界面会更容易。
interface Increment {
(): number;
}
var func = ((): Increment => {
var _count: number = 0;
var increment: Increment = function () {
_count += 1;
return _count;
}
return increment;
})();