简体   繁体   中英

TypeScript NodeJS application Error: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017)

This is time.ts file:

exports.getMinute = (timestamp: number) => {
    const milliSecond = timestamp * 1000;
    const dateObject = new Date(milliSecond);
    const minute = dateObject.toLocaleString('en-us',{minute : "number"});
    return minute;
};

exports.getHour = (timestamp: number) => {
    const milliSecond = timestamp * 1000;
    const dateObject = new Date(milliSecond);
    const hour = dateObject.toLocaleString('en-us',{hour : "number"});
    return hour;
};

exports.getDay = (timestamp: any) => {
    const milliSecond = timestamp ;
    const dateObject = new Date(milliSecond);
    
    const day = dateObject.getDate();
    return day;
};

exports.getMonth = (timestamp: any) => {
    const milliSecond = timestamp ;
    const dateObject = new Date(milliSecond);
    const month = dateObject.getMonth();
    return month;
};
exports.getYear = (timestamp: any) => {
    const milliSecond = timestamp ;
    const dateObject = new Date(milliSecond);
    const year = dateObject.getFullYear();
    return year;
};

exports.fullTime = (timestamp: any) => {
        let day = this.getDay(timestamp);
        const year = this.getYear(timestamp);
        let month = this.getMonth(timestamp);
        month ++
        return `${year}`+'-'+`${month<=9 ? '0'+ month : month }` + '-' + `${day<=9 ? '0'+ day : day }`;
};

I get the following errors:

Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017)

Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017)

Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017)

For these lines of code:

let day = this.getDay(timestamp);
const year = this.getYear(timestamp);
let month = this.getMonth(timestamp);

Also these errors:

Type '"number"' is not assignable to type '"numeric" | "2-digit" | undefined'.ts(2322)

Type '"number"' is not assignable to type '"numeric" | "2-digit" | undefined'.ts(2322)

At these lines of the code:

const minute = dateObject.toLocaleString('en-us',{minute : "number"});
const hour = dateObject.toLocaleString('en-us',{hour : "number"});

I don't know what does it mean and how can I fix them?

There are a few things you can do to remove the errors.

exports. is more of a javascript construct, which you usually don't use with typescript. So, I would replace it with export const in all cases in your file, for example:

export const getDay = (timestamp: any) => {
  const milliSecond = timestamp;
  const dateObject = new Date(milliSecond);

  const day = dateObject.getDate();
  return day;
};

Regarding usage of this , you usually want to use this when you are located in context of a class, but in your case you have separate functions. So, after changing all export statements, just call the functions directly:

  let day = getDay(timestamp);
  const year = getYear(timestamp);
  let month = getMonth(timestamp);

As for toLocaleString , this is typescript trying to tell you that the value number that you used for minute and hour properties is not a correct value that it supports, and it suggests that you replace it with either numeric , 2-digit , or just not define it (which defaults to undefined ). So, statements like this should work:

  const minute = dateObject.toLocaleString('en-us', { minute: 'numeric' });
  const hour = dateObject.toLocaleString('en-us', { hour: 'numeric' });

IDE usually also gives you hints, for example hovering over a property would tell you what type that property expects. In case of minute it looks like this , which is a union type of 2 string values or undefined. Try hovering over other properties to give you a better idea of what type they expect.

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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