Typescript / nodejs:变量在某些位置隐式具有类型“ any”

萨农

我正在使用带有nodejs的typescript来初始化数据库数据,我想声明一个全局数组变量以在函数内部使用:

export { };
import {Address,CodePostal} from 'api/models';
const faker = require('faker')
const _ = require('lodash')
const quantity = 20
var codes

async function setup() {
  const adminUser1 = new User(ADMIN_USER_1);
  await adminUser1.save();

  await seedCodesPostal()
}

async function checkNewDB() {
  const adminUser1 = await User.findOne({ email: ADMIN_USER_1.email });
  if (!adminUser1) {
    console.log('- New DB detected ===> Initializing Dev Data...');
    await setup();
  } else {
    console.log('- Skip InitData');
  }
}

const seedCodesPostal = async () => {
  try {
    var codesPostal = []
    for (let i = 0; i < quantity; i++) {
      codesPostal.push(
        new CodePostal({
          codePostal: faker.address.zipCode("####")
        })
      )
    }
    codesPostal.forEach(async code => {
      await code.save()
    })
  } catch (err) {
    console.log(err);
  }
  codes = codesPostal ***// here is the error : variable codes has implicitly type 'any' in some locations where its type cannot be determined ***//
}

const seedAddresses = async (codes: any) => {
  try {
    const addresses = []
    for (let i = 0; i < quantity; i++) {
        addresses.push(
          new Address({
            street: faker.address.streetName(),
            city: faker.address.city(),
            number: faker.random.number(),
            codePostal: _.sample(codes),
            country: faker.address.country(),
            longitude: faker.address.longitude(),
            latitude: faker.address.latitude(),
          })
        )
    }

  } catch (error) {

  }
}

checkNewDB();

我想将codePostal的内容放在函数变量中的seedCodesPostal中,并将其作为params传递给函数seedAddresses中。

如何将codes变量定义为CodesPostal正确性数组?

亚历克斯·韦恩

当您创建类似数组let arr = []的类型时,类型被推断为any[],因为Typescript不知道该数组中将包含什么。

因此,您只需要将该数组作为CodePostal实例数组输入即可

var codesPostal: CodePostal[] = []

您还需要codestry进行分配,否则codesPostal,如果catch被触发则永远无法设置

进行这些编辑后,您将在此处得到简化的代码:

const quantity = 20

// Added type here
var codes: CodePostal[] = []

class CodePostal {
  async save() { }
}

const seedCodesPostal = async () => {
    try {
        // Added type here.
        var codesPostal: CodePostal[] = []

        for (let i = 0; i < quantity; i++) {
            codesPostal.push(
                new CodePostal()
            )
        }
        codesPostal.forEach(async code => {
            await code.save()
        })

        // Moved assignment inside try block
        codes = codesPostal

    } catch (err) {
        console.log(err);
    }
}

操场

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Node + TypeScript:“ this”隐式具有类型“ any”

React Typescript:元素隐式地具有“ any”类型,因为类型没有索引签名

TypeScript / ReactJS / setTimeout:'this'隐式地具有'any'类型,因为它没有类型注释。

TypeScript:'this' 隐式具有类型 'any',因为它没有类型注释

变量“ a”隐式具有“ any []”类型

TypeScript + ES6 Map +对象类型的索引签名隐式具有“ any”类型

TypeScript:对象类型的索引签名隐式具有“ any”类型

当Typescript检查经典JS类时,如何解决“'this'隐式具有类型'any'”

react / typescript:参数“ props”隐式具有“ any”类型错误

Typescript错误:元素隐式具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

TypeScript:隐式具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

TypeScript - 元素隐式具有“any”类型,因为“storyFile”类型的表达式不能用于索引类型“{}”

Typescript-React State:元素隐式具有“ any”类型,因为类型“ State”没有索引签名

Typescript:TS7017对象类型的索引签名在循环中隐式具有“ any”类型

TypeScript TS7015:元素隐式地具有“ any”类型,因为索引表达式不是“ number”类型

变量 'data' 隐式具有类型 'any[]'

Vuex Typescript我收到错误“成员'someMutation'隐式具有'any'类型”。在Component.ts文件中

参数隐式具有“ any”类型

TypeScript-元素隐式地具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

map()中的Typescript错误:元素隐式具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

带有React> Element的Typescript隐式具有'any'类型,因为类型'string'的表达式不能用于索引

Object.keys迭代导致Typescript错误“元素隐式具有'any'类型,因为索引表达式不是'number'类型”

参数“ dispatch”隐式具有“ any”类型

函数隐式具有返回类型“ any”错误

为什么参数“ props”隐式具有“ any”类型?

错误消息:成员“ makes”隐式具有“ any”类型

绑定元素'Component'隐式具有'any'类型.ts

绑定元素“ title”隐式具有“ any”类型

参数“error”隐式具有“any”类型