繁体   English   中英

将js转换为ts时如何修复Typescript上的@ts-expect-error ts-migrate(2571)

[英]How to fix @ts-expect-error ts-migrate(2571) on Typescript when convert js to ts

我开始从打字稿开始,我试图将我的 JS 文件转换为 TS。

我有一个具有以下功能的课程

应用程序JS

async function get_stocks_saved_on_database(request: any, response: any) {

  let mongoDatabaseStockOperations = new MongoDatabaseStockOperations()
  let all_ocorrencies = await mongoDatabaseStockOperations.findAllOcorrencesOrderByValue(Constants.STOCKS_REPORT_COLLECTION)

  // @ts-expect-error ts-migrate(2571) FIXME: Object is of type 'unknown'.
  all_ocorrencies.forEach((element: any) => {
    response.write(element.stock_name + " | " + element.stock_value + " | " + element.percent_difference + " | " + element.day + "\n")    
  });
  response.end()
}

async findAllOcorrencesOrderByValue(my_investments_collection: any){
  console.log("start findAllOcorrences")
  return new Promise(function(resolve, reject) {
    // Do async job
    MongoDb.MongoClient.connect(url, function(err, db) {
      if (err) reject(err);
      var dbo = db?.db(database_name);
      var mysort:MongoDb.Sort = {  "day": -1, "percent_difference": -1 };
      dbo?.collection(my_investments_collection).find().sort(mysort).collation({locale: "en_US", numericOrdering: true}).toArray(function(err, result) {          
                
        if (err) reject(err);
        console.log(result);
        resolve(result)
        db?.close();
        
      });
    });
    
  });
  
}

我不知道如何识别“all_ocorrencies”的对象类型

let all_ocorrencies = await mongoDatabaseStockOperations.findAllOcorrencesOrderByValue(Constants.STOCKS_REPORT_COLLECTION)

函数 findAllOcorrencesOrderByValue 返回一个带有来自 mongodb 数据库对象的 Promise,但我不知道该对象的类型是什么。

我该如何解决?

unknown类型

错误2571是指跳过对类型unknown的类型检查。 进行类型检查将确认特定类型。

let x: unknown;

x.toString(); // Expected output: Error: Object is of type 'unknown'.
x + 1; // Expected output: Error: Object is of type 'unknown'.
Array[x] // Expected output: Error: Type 'unknown' cannot be used as an index type.

要证明 x 是特定类型,我们可以将其传递到 if 语句中

if (typeof x === 'string') x.toString(); // Inline if
if (typeof x === 'number') { // Multiline if
    x += 2;
}

通常鼓励确保智能感知或您的代码编辑器/IDE 的自动完成功能预测与类型相对应的方法和更多信息。

请注意任何具有unknown类型的变量,没有自动完成填充,而当手动类型检查时,它具有相关类型的所有自动完成功能。

执行

至于类型未知的特定对象,我不确定是哪个,但代码应该如下所示:

if (typeof x !== 'object') return;

all_ocorrencies的情况下:

if (typeof all_ocorrencies !== 'array') return; // Or handle it yourself
all_ocorrencies.forEach((element: any) => {
    response.write(element.stock_name + " | " + element.stock_value + " | " + element.percent_difference + " | " + element.day + "\n")    
});

element的情况下:

all_ocorrencies.forEach((element: unknown) => {
    if (typeof element !== 'object') return; // Or handle it yourself
    response.write(element.stock_name + " | " + element.stock_value + " | " + element.percent_difference + " | " + element.day + "\n")    
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM