小编Zac*_*ose的帖子

打字稿/ Javascript:使用元组作为Map的键

在我的代码中点击这个奇怪的错误,当使用元组作为我的密钥时,我无法想象从地图中获得恒定时间查找的方法.

希望这说明了这个问题,我现在正在使用的解决方法就是让它工作:

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()方法起作用,但没有取得多大成功.

javascript typescript

9
推荐指数
2
解决办法
5714
查看次数

如何使用WPF用户控件关闭父窗口

假设我有两个WPF窗口:window_One和window_Two.

  • window_One有一个按钮.单击此按钮将打开window_Two.
  • window_Two包含一个用户控件.
    • 该用户控件有一个关闭window_Two的按钮.

我该如何实现这种情况?

c# wpf user-controls wpf-controls

6
推荐指数
1
解决办法
9405
查看次数

Jest/Jasmine:beforeEach的奇怪执行顺序()

鉴于课程:

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)

testing unit-testing jasmine typescript jestjs

6
推荐指数
1
解决办法
1206
查看次数

Typescript、typeof 等价于方法签名?

很好奇是否有办法确定方法的签名。希望这段代码能说明这个问题:

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)

typescript

5
推荐指数
1
解决办法
1867
查看次数

*ngFor 循环中的解构赋值

尝试在我的标记中使用*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 angular

4
推荐指数
2
解决办法
3025
查看次数