我以以下方式实现了一个TypeScript枚举:
export class Size{
static SMALL = new Size('smaller than 100', 1, 100);
static MEDIUM = new Size('smaller than 1000', 1, 1000);
readonly description: string;
readonly from: number;
readonly to: number;
private constructor(description
假设我有两个类(或更多)。一个作为数据库实体,一个作为json约束。因为数据库实体将属性设置为外键,因此属性是对象。但在json的情况下,该属性只是一个字符串类型。
interface A {
title: string
catalogue: string
}
数据库实体类需要目录作为对象,因为CatObj包含其他信息,如id、名称等。
class AEntity implements A {
public title: string
public catalogue: CatObj
}
json格式
const aJson: A = {
title:
我正在尝试使用Enum值作为对象的键,希望它在得到值时保留类型,但我得到的是Element implicitly has an 'any' type because expression of type 'string' can't be used to index type。
export enum TaskType {
Classification = 'classification',
Extraction = 'extraction'
}
const comparisons: { [name in Task
在TypeScript中,如何遍历类的属性?以下面的类为例:
export class Task implements Itask {
public Id: number = 0;
public Name: string;
public Description: string;
public Completed: boolean = false;
public TaskType: TaskType;
}
我想检索属性,因此:"Id",“Name","Description","Completed","TaskT
我有一个变量,比方说gender。我在开始时声明它,稍后在代码中赋值。我希望将gender的值限制为male或female。除此之外的任何东西都不应该被接受(抛出错误)。在typescript中有没有办法做到这一点? let gender: string;
// some logic here
gender = someFunction(); //Restrict this to only accept 'male' and 'female' as valid values.
我有一个枚举,我想像解析对象一样解析它。我真的很喜欢Swift编程语言,你只需要使用.rawValue来获取枚举成员(i.e. HelloWorld.one.rawValue = "this is one")的字符串值,但是我还不能理解如何在Typescript中做到这一点。 enum HelloWorld {
one = "this is one",
two = "two",
three = "the third"
}
const getStringFromEnum = (num: HelloWor
我想创建一个通用的TypeScript类,用于呈现(作为HTML )实现特定interface的对象数组。
例如:
class GenericListRenderer<T> {
items: T[];
constructor(listItems: T[], className?: string){
this.items = listItems;
...
}
private getPropertyNames(): string[]{
// What is the best way to access all property
我有一个函数,它接受一个参数,该参数的类型在typescript中有几个不同的枚举。我想检查参数是哪种枚举类型。有没有可能检查一下?下面是一个示例代码:
enum X {
ONE,
TWO
}
enum Y {
THREE,
FOUR
}
function check(arg1: X | Y) {
if (arg1 is X) {
// do something with X
}
if (arg1 is Y) {
// do something with Y
}
}
我在这个网站上学习收集框架:。在阅读了Hashtable的所有方法之后,我看到了两个访问表键的方法:
Set keys():返回包含所有键的Set对象
Set keySet():返回一个包含Hashtable所有键的Set对象。一个相似之处是Hashtable和Set不允许重复。在Set中添加和删除元素也反映在Hashtable中
它们都返回一个Set对象。我看不出他们之间有什么不同。有人能告诉我这个吗?
我收到一个奇怪的TypeScript错误。
我有下面的例子:
interface Foo {
prop: 0 | 1 | 2;
}
class Bar implements Foo {
prop = 1;
}
我得到了一个错误:
src/example.ts:6:3 - error TS2416: Property 'prop' in type 'Bar' is not assignable to the same property in base type 'Foo'.
Type 'number' is not a
我有一个这样的TypeScript接口: export interface Foo {
bar: string
} 这将编译成一个空的.js文件。因此,当在ES6节点模块中使用TypeScript接口时,我会得到以下错误: TypeError: Class extends value undefined is not a function or null 解决方法是创建一个类 export class BaseFoo {
bar: string
} 现在,我可以在ES6中使用BaseFoo,而不会出现上述错误。因此,在TypeScript中使用ES6互操作不可能只有接口,或者有没有比