我这样定义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)的错误,还是我错过了什么?
我在 tsconfig.json 中打开了严格模式。我无法按照编译器的意愿改变代码的某些部分。有没有一种方法可以降低严格性来标记特定文件或更好的代码块,而不是关闭整个项目的设置?
所以我在想这样的事情。
function foo() {
// implicitAny not allowed here
/// noImplicitAny false
// implicitAny allowed here
/// noImplicitAny true
}
Run Code Online (Sandbox Code Playgroud)
我认为三重破折号指令可能会有所帮助,但它们似乎没有帮助。
谢谢 :-)
我正在研究我的第一个 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”类型参数的索引签名。邀请:字符串;用户:字符串;身份验证:字符串;社交:字符串;}'
主要问题:有人可以帮我弄清楚我在这里做错了什么吗?