在TypeScript类中,可以为属性声明类型,例如:

class className {
  property: string;
};

如何在对象文字中声明属性的类型?

我已经尝试了以下代码,但它不能编译:

var obj = {
  property: string;
};

我得到以下错误:

名称“string”在当前作用域中不存在

是我做错了什么还是这是个bug?


当前回答

我很惊讶没有人提到这一点,但你可以创建一个名为ObjectLiteral的接口,它接受类型string: any的键:值对:

interface ObjectLiteral {
  [key: string]: any;
}

然后你可以这样使用它:

let data: ObjectLiteral = {
  hello: "world",
  goodbye: 1,
  // ...
};

一个额外的好处是,您可以根据需要在任意多个对象上多次重用该接口。

祝你好运。

其他回答

只是扩展@RickLove的回复…

这很好,因为你只需要定义不能推断的类型:

const initialState = { 
   user: undefined as User | undefined, 
   userLoading: false
}; 

它被编译成下面的js代码:

const initialState = { 
   user: undefined, 
   userLoading: false
};  

如果你需要把它提取成一个类型,你可以这样做:

export type InitState = typeof initialState;

更新2019-05-15(可选的改进代码模式)

在使用const多年并受益于更多函数式代码之后,我建议在大多数情况下不要使用我最初的答案(本节下面的标题,即在构建对象时,将类型系统强制为特定类型而不是让它推断类型通常表明有错误)。

相反,我建议尽可能使用const变量,然后在最后一步组合对象:

const id = getId();
const hasStarted = true;
...
const hasFinished = false;
...
return { hasStarted, hasFinished, id };

这将正确地键入所有内容,而不需要显式键入。 不需要重新键入字段名。 从我的经验来看,这导致了最干净的代码。 这允许编译器提供更多的状态验证(例如,如果你在多个位置返回,编译器将确保总是返回相同类型的对象——这鼓励你在每个位置声明整个返回值——给出该值的完全明确的意图)。

附加信息:可选字段2022-09-29

const id = getId();
const optionalField = getOptionalValue();
return {
    id,
    // This will always exist as a key in the object but it might be undefined
    optionalField,
    // This will only exist as a key in the object if it has a truthy value
    ...optionalField2 ? { optionalField } : {},
    // This will only exist as a key in the object if it is not null or undefined
    ...optionalField2 != null ? { optionalField } : {},
};

除了2020-02-26

如果你确实需要一个可以延迟初始化的类型:标记它是一个可空的联合类型(null或type)。类型系统将阻止您在未首先确保它有值的情况下使用它。

在tsconfig。Json,确保你启用了严格的null检查:

“strictNullChecks”:true

然后使用这个模式,并允许类型系统保护你免受意外的null/undefined访问:



const state = {
    instance: null as null | ApiService,
    // OR
    // instance: undefined as undefined | ApiService,

};

const useApi = () => {
    // If I try to use it here, the type system requires a safe way to access it

    // Simple lazy-initialization 
    const api = state?.instance ?? (state.instance = new ApiService());
    api.fun();

    // Also here are some ways to only access it if it has value:

    // The 'right' way: Typescript 3.7 required
    state.instance?.fun();

    // Or the old way: If you are stuck before Typescript 3.7
    state.instance && state.instance.fun();

    // Or the long winded way because the above just feels weird
    if (state.instance) { state.instance.fun(); }

    // Or the I came from C and can't check for nulls like they are booleans way
    if (state.instance != null) { state.instance.fun(); }

    // Or the I came from C and can't check for nulls like they are booleans 
    // AND I was told to always use triple === in javascript even with null checks way
    if (state.instance !== null && state.instance !== undefined) { state.instance.fun(); }
};

class ApiService {
    fun() {
        // Do something useful here
    }
}

99%的情况下不要做以下事情:

更新2016-02-10 -处理TSX(谢谢@Josh)

对TSX使用as操作符。

var obj = {
    property: null as string
};

一个更长的例子:

var call = {
    hasStarted: null as boolean,
    hasFinished: null as boolean,
    id: null as number,
};

原来的答案

使用强制转换操作符可以简化此操作(通过将null强制转换为所需类型)。

var obj = {
    property: <string> null
};

一个更长的例子:

var call = {
    hasStarted: <boolean> null,
    hasFinished: <boolean> null,
    id: <number> null,
};

这比分成两部分(一部分声明类型,另一部分声明默认值)要好得多:

var callVerbose: {
    hasStarted: boolean;
    hasFinished: boolean;
    id: number;
} = {
    hasStarted: null,
    hasFinished: null,
    id: null,
};

使用type关键字创建类型

type ObjType = {
  property: string;
}

然后,您可以使用它来绑定您的对象,以只接受这种类型,如下所示。

const obj: ObjType = {
property: "TypeScript"
}
// Use ..

const Per = {
  name: 'HAMZA',
  age: 20,
  coords: {
    tele: '09',
    lan: '190'
  },
  setAge(age: Number): void {
    this.age = age;
  },
  getAge(): Number {
    return age;
  }
};
const { age, name }: { age: Number; name: String } = Per;
const {
  coords: { tele, lan }
}: { coords: { tele: String; lan: String } } = Per;

console.log(Per.getAge());

在TypeScript中,如果我们声明object,那么我们将使用以下语法:

[access modifier] variable name : { /* structure of object */ }

例如:

private Object:{ Key1: string, Key2: number }