TypeScript 变量 “implicitly has type ‘any’ because it does not have a type annotation”

huangapple go评论53阅读模式
英文:

TypeScript variable "implicitly has type 'any' because it does not have a type annotation"

问题

错误:

> 'environment' 因为没有类型注释并且在其自己的初始化程序中直接或间接引用,隐式具有类型 'any'。

代码:

export const environment = {
    production: false,
    firebaseConfig: {
        apiKey: "myKey",
        authDomain: "domain",
        projectId: "id",
        storageBucket: "storageUrl",
        messagingSenderId: "59142751602",
        appId: "a:b:web:c"
    },
};
英文:

Error:

> 'environment' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

Code:

export const environment = {
    production: false,
    firebaseConfig : {
        apiKey: "myKey",
        authDomain: "domain",
        projectId: "id",
        storageBucket: "storageUrl",
        messagingSenderId: "59142751602",
        appId: "a:b:web:c"
    },
};

答案1

得分: 0

只需在tsconfig.json文件中添加以下选项:

"noImplicitAny": false,

禁用此功能并不是一个好主意,但Angular不允许运行项目,它会显示错误。

有关“noImplicitAny”选项的更详细说明,请参阅此处

英文:

Just add below option in the tsconfig.json file:

"noImplicitAny": false,

Disabling the feature is not good idea but angular not allowed to run the project it shows error.

For more detailed explanation about the "noImplicitAny" option.

答案2

得分: 0

你只需将 any 类型简单地添加到以下代码行中:export const environment: any = { // 在此处填写详细信息 }。 接受的答案完全有效,但这将禁用整个项目的类型检查,公平地说,使用TypeScript的优势是什么?

英文:

You can simply add any type to the line export const environment: any = {
// details go here }
. The accepted answer works just fine, however, it will disable type checking across the whole project and being fair, what's the advantage of using Type Script?

huangapple
  • 本文由 发表于 2023年7月14日 04:30:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定