我对Typescript中的Node开发有一个担忧。我一直在与一位精通node和javascript的node开发人员合作,但现在我们决定从javascript转向typescript,这是一个很好的决定。
问题是,因为我们已经开始用typescript编写代码,但是我们面临着选择是在typescript中使用模块化方式还是使用类的冲突。
选项1:
ab.ts
export function a () {
will write whole code here for that
}
选项2:
ab.ts
export class Ab {
constructor(){
}
public a
我正在尝试创建一个具有以下结构的小型库:
library('input').method()
这是代码( addQuotes方法向string输入添加引号):
const library = function (text: string) {
interface ValueObject {
addQuotes?: Function
} // Without this, I get "Property 'to' does not exist on type '{}'."
我这样定义一个AbstractModel:
export interface AbstractModel {
[key: string]: any
}
然后我将类型声明为Keys
export type Keys = keyof AbstractModel;
我希望任何具有Keys类型的内容都会被统一地解释为一个字符串,例如:
const test: Keys;
test.toLowercase(); // Error: Property 'toLowerCase' does not exist on type 'string | number'. Pro
我遇到了这样的代码:
declare type A = { ... };
declare interface B { ... }
从搜索declare关键字(并且没有任何点击因为某种原因指向我TypeScript文档),当JavaScript中存在变量时使用declare,并且需要从TypeScript引用它,因此您向编译器声明这个变量确实存在。
我发现使用此关键字的所有示例如下:declare var a;
为什么我会在type或interface面前使用它?根据我对declare的理解,这样做是完全没用的,但是编译器不会给我任何错误。
在普通的JavaScript中,我们可以使用变量读取对象的属性值。
也就是说,这是有效的:
let obj = { a: 100, b: 'Need help with TypeScript', c: new Date() };
let prop = 'b';
console.log( obj[prop] ); // Need help with TypeScript
但是,下面的TypeScript注释声明prop为字符串将导致指示的索引错误。
let obj: object = { a: 100, b: 'Need help with TypeSc
我有一个具有以下结构的JavaScript对象:
{
"_index": 1,
"foo": { ... },
"bar": { ... },
"baz": { ... },
}
也就是说,对象总是有一个带有数字值的属性_index,然后所有其他键(因为它被用作映射而没有定义)都有另一个特定对象的值。
我在TypeScript中尝试了以下类型
type Properties = {
_index: number
}
type Fields = {
// its not important what Me
typescript中与javascript中的以下代码行在语义上等价的语法是什么
//Some knockout event handler.
myFunc(data : string, evt : Event) {
//If enter or tab key up were detected add the excuse to the collection.
if(evt.enterKey || evt.which == 9)
//Do Something
}
这里我遇到的麻烦不同于常规的javascript事件,typescript事件类没有enterKe