相关疑难解决方法(0)

推断字符串的关键字 当key只是一个字符串时的数字

我这样定义AbstractModel:

export interface AbstractModel {
   [key: string]: any
}
Run Code Online (Sandbox Code Playgroud)

然后我宣布类型Keys:

export type Keys = keyof AbstractModel;
Run Code Online (Sandbox Code Playgroud)

我希望任何具有Keys类型的东西都可以单义地解释为字符串,例如:

const test: Keys;
test.toLowercase(); // Error: Property 'toLowerCase' does not exist on type 'string | number'. Property 'toLowerCase' does not exist on type 'number'.
Run Code Online (Sandbox Code Playgroud)

这是一个Typescript(2.9.2)的错误,还是我错过了什么?

typescript typescript-typings

18
推荐指数
3
解决办法
3872
查看次数

您可以在单个文件或代码块上关闭严格模式等吗?

我在 tsconfig.json 中打开了严格模式。我无法按照编译器的意愿改变代码的某些部分。有没有一种方法可以降低严格性来标记特定文件或更好的代码块,而不是关闭整个项目的设置?

所以我在想这样的事情。

function foo() {
 // implicitAny not allowed here 

/// noImplicitAny false

 // implicitAny allowed here 

/// noImplicitAny true
}
Run Code Online (Sandbox Code Playgroud)

我认为三重破折号指令可能会有所帮助,但它们似乎没有帮助。

谢谢 :-)

typescript

8
推荐指数
1
解决办法
8202
查看次数

在类型 ' 上找不到带有类型为 'string' 的参数的索引签名

我正在研究我的第一个 firebase typescript 函数项目

我有以下代码片段。

const files = {
    status: './src/api/status.f.js',
    invite: './src/api/invite.f.js',
    user: './src/api/user.f.js',
    auth: './src/api/auth.f.js',
    social: './src/api/social.f.js'
}

for (let file in files)
    if (files.hasOwnProperty(file)) {
        // Setting function name equivalent to the file name
        if (!process.env.FUNCTION_NAME || process.env.FUNCTION_NAME === file) {
            const ApiClass = require(files[file])
            const app = new ApiClass(context)
            exports[file] = app.handler
        }
    }
Run Code Online (Sandbox Code Playgroud)

在这一点上,在这一行我收到以下错误 const ApiClass = require(files[file])

元素隐式具有 'any' 类型,因为类型 'string' 的表达式不能用于索引类型 '{ status: string; 邀请:字符串;用户:字符串;身份验证:字符串;社交:字符串;}'。在类型“{ status: string;”上找不到带有“string”类型参数的索引签名。邀请:字符串;用户:字符串;身份验证:字符串;社交:字符串;}'

主要问题:有人可以帮我弄清楚我在这里做错了什么吗?

typescript

7
推荐指数
1
解决办法
3626
查看次数

标签 统计

typescript ×3

typescript-typings ×1