typescript1.4

TypeScript: use of generic and union types

房东的猫 提交于 2021-01-29 04:15:50
问题 I have code which uses function overload, something like: function f0(v: Object): void { } function f1(a: number, b: number): void; function f1(a: string, b: string): void; function f1(a: any, b: any): void { f0(a); // OK } That I would like to convert using union types, but I get an error: function f1<T extends number | string>(a: T, b: T): void { f0(a); // Error: Argument of type 'T' is not assignable to parameter of type 'Object'. } What's wrong here, knowing that the following is OK:

TypeScript: use of generic and union types

十年热恋 提交于 2021-01-29 04:08:20
问题 I have code which uses function overload, something like: function f0(v: Object): void { } function f1(a: number, b: number): void; function f1(a: string, b: string): void; function f1(a: any, b: any): void { f0(a); // OK } That I would like to convert using union types, but I get an error: function f1<T extends number | string>(a: T, b: T): void { f0(a); // Error: Argument of type 'T' is not assignable to parameter of type 'Object'. } What's wrong here, knowing that the following is OK:

Why can't I do instanceof on a union?

对着背影说爱祢 提交于 2021-01-26 17:38:29
问题 It won't allow an instanceof in this case - why? public assign(color: string | ColorProperty | RgbProperty | RgbColor): void { super.assign(color); if (color instanceof ColorProperty) { ps - I LOVE unions!!! 回答1: It seems that the instanceof keyword must have a type any included in the union in order to use it within a function. My guess would be that the compiler needs to handle the case where all of your type guards return false - and therefore the type is inferred as any . function assign(

Why can't I do instanceof on a union?

怎甘沉沦 提交于 2021-01-26 17:37:24
问题 It won't allow an instanceof in this case - why? public assign(color: string | ColorProperty | RgbProperty | RgbColor): void { super.assign(color); if (color instanceof ColorProperty) { ps - I LOVE unions!!! 回答1: It seems that the instanceof keyword must have a type any included in the union in order to use it within a function. My guess would be that the compiler needs to handle the case where all of your type guards return false - and therefore the type is inferred as any . function assign(

Why can't I do instanceof on a union?

让人想犯罪 __ 提交于 2021-01-26 17:36:19
问题 It won't allow an instanceof in this case - why? public assign(color: string | ColorProperty | RgbProperty | RgbColor): void { super.assign(color); if (color instanceof ColorProperty) { ps - I LOVE unions!!! 回答1: It seems that the instanceof keyword must have a type any included in the union in order to use it within a function. My guess would be that the compiler needs to handle the case where all of your type guards return false - and therefore the type is inferred as any . function assign(

Why can't I do instanceof on a union?

ⅰ亾dé卋堺 提交于 2021-01-26 17:35:17
问题 It won't allow an instanceof in this case - why? public assign(color: string | ColorProperty | RgbProperty | RgbColor): void { super.assign(color); if (color instanceof ColorProperty) { ps - I LOVE unions!!! 回答1: It seems that the instanceof keyword must have a type any included in the union in order to use it within a function. My guess would be that the compiler needs to handle the case where all of your type guards return false - and therefore the type is inferred as any . function assign(

Angular detect if a popup window is opened

ぐ巨炮叔叔 提交于 2020-07-09 11:48:54
问题 In my board (written in Angular 4), the dialogues are opened like below: Importing ActivatedRoute, Router import { ActivatedRoute, Router } from '@angular/router'; Adding Router and ActivatedRoute references in constructor constructor ( ... private _router: Router, private _activatedRoute: ActivatedRoute, ...) And Using it in below way this._router.navigate(['./', { outlets: { somemodal: 'somemodal', dialogs: null }, relativeTo: this._activatedRoute }]); Requirement is when you hit escape Esc

Typing an Array with a union type in TypeScript?

和自甴很熟 提交于 2019-12-30 08:05:28
问题 I was just wondering if it is possible to type array with a union type, so that one array can contain both Apples and Oranges but nothing else. Something like var arr : (Apple|Orange)[] = []; arr.push(apple); //ok arr.push(orange); //ok arr.push(1); //error arr.push("abc"); // error Needless to say, the example above does not work so this may not be possible, or am I missing something? 回答1: class Apple { appleFoo: any; } class Orange { orangeFoo: any; } var arr : Array<Apple|Orange> = []; var

Typing an Array with a union type in TypeScript?

穿精又带淫゛_ 提交于 2019-12-30 08:05:15
问题 I was just wondering if it is possible to type array with a union type, so that one array can contain both Apples and Oranges but nothing else. Something like var arr : (Apple|Orange)[] = []; arr.push(apple); //ok arr.push(orange); //ok arr.push(1); //error arr.push("abc"); // error Needless to say, the example above does not work so this may not be possible, or am I missing something? 回答1: class Apple { appleFoo: any; } class Orange { orangeFoo: any; } var arr : Array<Apple|Orange> = []; var

AngularJs+TypeScript:-How to get the selected index value in ng-select

ぃ、小莉子 提交于 2019-12-23 05:42:18
问题 I m working on module where I have check a condition for the visibility of a panel as if slected index of a dropdown is not equal to 0 or -1 than it will make visible to my panel else it will not. I am bit confused as how to get the selected index value of my dropdown using ng-select . I have shown my dropdown code below:- <div data-ng-app="CustomerNew" data-ng-controller="CreateCustomerCtrl as custom" ng-init= getFormData(); > <table> <tr> <td> <select id="accountselector" style="width: 100%