microsoft / microsoft/TypeScript
--noImplicitAny codefixes infer anonymous object types despite appropriate interfaces in (or out of) scope
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.3.0-dev.20181212
Search Terms: noimplicitany infer code fix codefix quick suggested
Code
interface IBox {
x: number;
y: number;
}
const shiftBox = (box) => {
box.x += 1;
box.y += 1;
}
const box = {
x: 0,
y: 0,
};
shiftBox(box);
Expected behavior:
--noImplicitAny suggested fix on the box parameter:
const shiftBox = (box: IBox) =>
Actual behavior:
const shiftBox = (box: { x: any; y: any; }) => {
Related Issues: #13243 (parent tracking quick fixes) and #15114 (discussion on inference difficulties)
If there is an interface available that can satisfy an inferred type in a --noImplicitAny code fix, can we use that? Perhaps with an ordering of possibilities like:
- Interfaces already available in the file, by how small they are
- User-defined interfaces that could be imported, by how distant the file is
- Module types already imported in a user file, by how distant the nearest import is
...where, if multiple interfaces could satisfy the best possibility of those three, we choose the one with the fewest fields?
In code bases that don't explicitly type when unnecessary (e.g. : IBox for variables), I'm finding the --noImplicitAny fixes to be a bit useless for anything other than primitives.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Begin with the supplied TypeScript example and reproduce the --noImplicitAny codefix output. Read the discussion in related issues #13243 and #15114 to understand the proposed interface-selection rules; done means a compatible available interface such as IBox is preferred over an anonymous object type without breaking other codefix cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100