在我的代码中点击这个奇怪的错误,当使用元组作为我的密钥时,我无法想象从地图中获得恒定时间查找的方法.
希望这说明了这个问题,我现在正在使用的解决方法就是让它工作:
hello.ts:
let map: Map<[number, number], number> = new Map<[number, number], number>()
.set([0, 0], 48);
console.log(map.get([0,0])); // prints undefined
console.log(map.get(String([0, 0]))); // compiler: error TS2345: Argument of type
// 'string' is not assignable to parameter of type '[number, number]'.
//the work-around:
map.forEach((value: number, key: [number, number]) => {
if(String(key) === String([0, 0])){
console.log(value); // prints 48
}
})
Run Code Online (Sandbox Code Playgroud)
要编译(transpile?)我正在使用:
tsc hello.ts -target es6
Run Code Online (Sandbox Code Playgroud)
tsc版本2.1.6
尝试了几件事来使Map.get()方法起作用,但没有取得多大成功.
假设我有两个WPF窗口:window_One和window_Two.
我该如何实现这种情况?
鉴于课程:
export class Foo {
private static _count = 0;
id: number;
constructor() {
this.id = ++Foo._count; // starts with 1
}
}
Run Code Online (Sandbox Code Playgroud)
和测试套件:
describe('Foo Class', () => {
describe('constructor', () => {
const fooClassRef = Foo as any; // to pass typechecking
beforeEach(() => {
console.log(`start of beforeEach: ${fooClassRef._count}`);
fooClassRef._count = 0;
console.log(`end of beforeEach: ${fooClassRef._count}`);
});
describe('creating one Foo obj', () => {
console.log(fooClassRef._count);
const foo = new Foo();
it('should have an id of 1', () => { …Run Code Online (Sandbox Code Playgroud) 很好奇是否有办法确定方法的签名。希望这段代码能说明这个问题:
class MyClass {
constructor(public foo: any){}
}
const object1 = new MyClass((): void => {
console.log('My function is to say hi. Hello!');
});
const object2 = new MyClass((n: number): void => {
console.log('My function is echo a number. Here it is: ' + n);
});
object1.foo(); // My function is to say hi. Hello!
object2.foo(15); // My function is echo a number. Here it is: 15
console.log(typeof object1.foo); // prints 'function'.
// I'm looking for something that prints '(): …Run Code Online (Sandbox Code Playgroud) 尝试在我的标记中使用*ngForagainst循环遍历 JS 字典Object.entries并收到错误消息:
解析器错误:在 Object.entries(items) 的 [let [key, item] 中的第 5 列出现意外标记 [、预期标识符、关键字或字符串]
模板:
<button *ngFor="let [key, item] of Object.entries(items)"
(click)="itemClicked.emit([key, item.value])">
{{ item.displayName }}
</button>
Run Code Online (Sandbox Code Playgroud)
打字稿:
export interface DropDownItem {
displayName: string,
value: any,
checked: boolean
}
@Component({ /* ... */ })
export class MyComponent {
@Input() items: { [key: string]: DropdownItem };
@Output() itemClicked = new EventEmitter<[string, any]>();
Object = Object;
constructor() { }
}
Run Code Online (Sandbox Code Playgroud) typescript ×4
angular ×1
c# ×1
jasmine ×1
javascript ×1
jestjs ×1
testing ×1
unit-testing ×1
wpf ×1
wpf-controls ×1