为什么TypeScript不缩小数组的类型?
function test(input: (string | number)[]): string[] {
// The type of .map(...) reports that it returns string[].
input = input.map(x => x.toString())
// Type error: Type '(string | number)[]' is not assignable to type 'string[]'.
return input
}
解决方法并
我正在使用VS Code,它在使用Typescript时非常有用,因为它告诉您当前的问题不需要传输。我使用的是Typescript和nodeJS,它工作得很好。
我唯一的问题就是所谓的“空/未定义的检查函数”。
参见此示例:
class User {
public someProperty: string | undefined;
// ...
public get canDoSomething() {
return this.someProperty != undefined;
}
}
let myUser: User = ...
这样做很好:
if (
以下代码曾与TypeScript 2.0配合使用:
function enumDemo() {
enum temperature{
cold,
hot
};
let temp = temperature.cold;
switch (temp) {
case temperature.cold:
console.log("Brrr...");
break;
case temperature.hot:
console.log("yikes!"
为什么类型检查不适用于someCat2??
interface A{
x: number;
}
function test(something: A){}
let someCat = {cat : "miao"}
function MakeCat(){
this.cat = "miao";
}
let someCat2 = new MakeCat();
test(someCat2); // --->> typescript DOES NOT marks the error <<-------
test(some
我正在寻找一种方法,在对象被隐式转换为字符串的情况下自动使用对象的toString()方法。例如,假设你有这样一个类: class Dog {
name: string;
constructor(name: string) {
this.name = name;
}
public toString(): string {
return `${this.name} is my friend`;
}
} 然后我会发现这个测试中两个断言中的第二个将失败: test.only("Dog", () => {
const dog = new
我有一个场景,我需要在构造函数外部初始化一个类属性。考虑下面的例子。 class A {
public prop: MyPropType;
public id: string;
public constructor(id: string) {
this.id = id;
}
public init(value: string): void {
this.prop = new MyPropType(value);
}
public toString(): string {
return
如何在Array'的类型签名中指定TypeScript的长度?
declare const a: Array<any>[20]; // wishful syntax
...
foo(a[10]); // fine
foo(a[100]); // type error: index is out of bounds
如果调用TypeScript,是否可以使用BigNumber.toString触发编译时错误?
具体来说,我的应用程序遇到了错误,因为BigNumber.toString()会用科学符号表示足够大和足够小的数字:
> x = new BigNumber('0.00000000001')
> x.toString()
'1e-10'
> x.toFixed()
'0.00000000001'
我试过推翻这种类型:
import {BigNumber} from "bignumber.js";
declare mo
我希望在下面的代码中有一个类型错误,但是typescript是完全可以的,你能告诉我为什么吗?
export interface Structure {
aaa: string;
}
export function f1(): Structure[] { // OK for typescript, not for me
const result = [].map(certState => {
return {
aaa: 'aaa',
ADDITIONAL_FIELD: 'asdf&
给定以下代码片段
let x = {
name: 'John',
age: 30
}
interface Employee {
name:string,
}
let y:Employee = <Employee>x;
console.log(y); //y still have the age property, why
为什么typescript会忽略这个类型转换,有没有其他的解决方案来只获取这些接口属性?
我使用的是TypeScript 3.4,下面的代码有一个奇怪的问题
interface MyInterface {
fn: (x: number) => number;
}
abstract class A {
abstract prop: MyInterface;
}
class B extends A {
prop = { fn: x => x }; // complains about x having implicit any here
}
TypeScript抱怨我的函数有一个隐式参数。现在当我写B类的时候
class B extends A {
pr
我有一个如下的json数据。我正在使用angular2 typescript根据日期范围过滤这个json。我使用Moment来格式化日期字符串。我需要根据类的所有属性过滤搜索值。我处理了其他的属性。我不知道如何过滤日期范围。有没有人能帮我解决这个问题呢?
过滤条件:从‘moment’导入* as moment;
export class FilterCriteria {
public from: string;
public to: string;
public searchValue: string;
constructor() {
this.
TypeScript拒绝编译debounce函数,因为包装函数的类型有问题:
export function debounce<F extends ((...args: any[]) => void)>(fn: F, timeout: number): F {
let timer: NodeJS.Timeout | undefined
// Problem here, TypeScript complains it's not the same function as F
return ((...args: any[]) => {
if (
为什么N和NN低于不同的类型?
type NonFunctionPropertyNames< T > = {
[ K in keyof T ]: T[ K ] extends Function ? never : K
}[ keyof T ];
//
// This is number | (() => string) | (() => string) | (() => number | undefined) | ...
//
type N = NonFunctionPropertyNames< string[] >;
//
// Th