plantain-00 / type-coverage

A CLI tool to check type coverage for typescript code
MIT License
1.21k stars 43 forks source link

'type is any' issue thrown from plugin but not from CLI #87

Closed geochronology closed 3 years ago

geochronology commented 3 years ago

Version(if relevant): 1.0.0

Environment(if relevant): vscode

Code(if relevant):

// test.ts (no issues here)

import {
  introducePerson,
  Status,
  isFulltimeEmployee,
  personToString,
  getProfessions,
} from "./objects";

const jack = {
  name: {
    first: "Jack",
    last: "Herrington",
  },
  status: Status.FullTime,
  profession: "Engineer",
};

console.log(introducePerson(jack));

console.log(isFulltimeEmployee(jack));

console.log(personToString(jack));

console.log(
  getProfessions({
    1: jack,
  })
);

// objects.ts (issue is here)

export const FullTime = "FullTime";
export const Temporary = "Temporary";

export enum Status {
  FullTime,
  Temporary
}

type Person = {
  name: {
    first: string,
    middle?: string
    last: string,
  },
  status: Status,
  profession: string
}

export const introducePerson = (person: Person) =>
  `Hello ${person.name.first} ${person.name.middle} ${person.name.last}`;

export const isFulltimeEmployee = (person: Person) => person.status === Status.FullTime;

export const personToString = (
  person: Person = {
    status: Status.FullTime,
    name: {
      first: "unknown",
      last: "unknown",
    },
    profession: "unknown",
  }
) => JSON.stringify(person, null, 2);

type PersonMap = { [key: number]: Person }

export const getProfessions = (personMap: PersonMap) =>
  Object.values(personMap)
    .map(({ profession }) => profession) // <=== this is the line that throws "The type of 'profession' is 'any'" in vscode
    .join("\n");

Expected: consistent results with plugin and npm module

Actual: Since type-coverage results in a 100% score using the above code, either the plugin is flagging this line inaccurately OR the type-coverage npm module is failing to catch it.

plantain-00 commented 3 years ago

It is any as typescript suggests:

屏幕快照 2021-03-11 17 26 59

The Object.values() is the root cause, I can fix it by add tsconfig.json in the project root directory:

{
    "compilerOptions": {
        "target": "es2017"
    }
}
屏幕快照 2021-03-11 17 29 48

You can try to add tsconfig.json (extends your CLI tsconfig.json) in the project root directory. I will add it in the document later.

geochronology commented 3 years ago

Thanks for clarifying!