tamj0rd2/ts-quickfixes

Support Arrays of types

wesbos opened this issue ยท 5 comments

First off, thanks a million for the extension! super handy.

I've found an array of type doesn't work. Take this code:

interface Job {
  title: string;
  location: string;
}

interface Person {
  name: string;
  age: number;
  job: Job[];
}

const wes: Person = {};

If I change Person Job[] to Job, it does do the nested values.

Could it work for arrays of Job as well?

Hey, thanks for the feedback. If you trigger the quick fix on your wes variable, this should be the result:

const wes: Person = {
    name: 'todo',
    age: 0,
    job: []
};

I'm not sure if it should automatically create a Job object inside of that array. Some people might not expect that kind of behaviour. Maybe this would be a better solution?

  1. You use the declare missing members fix to fill out the missing members, same as above
  2. You write an empty object inside of the job array, like this:
const wes: Person = {
    name: 'todo',
    age: 0,
    job: [{}]
};
  1. You trigger the declare missing members fix for that empty object, resulting in something like this:
const wes: Person = {
    name: 'todo',
    age: 0,
    job: [{ title: 'todo', location: 'todo' }]
};

Does that sound like a decent compromise? It doesn't work right now, but it's something I'd be happy to add when I have time, or accept as a PR contribution.

Oh! I was trying to trigger it inside the object. It does work like you have said.

I'm not seeing how to trigger it inside to create the Job missing members though, does my cursor need to be on the job part of job: [] ?

I think option 3 would be what I expect. I don't think that would create an unexpected results, but I haven't totally thought it trough

Ok cool! I don't think I explained it so well. The 3 points I wrote above were supposed to be the steps you'd ideally take to do what you're asking for, rather than 3 different solutions.

I'm not seeing how to trigger it inside to create the Job missing members though, does my cursor need to be on the job part of job: [] ?

const wes: Person = {
    name: 'todo',
    age: 0,
    job: [{}]
};

At this stage you would put your cursor inside of {} to trigger the quick fix, but it doesn't work at the moment. There needs to be a code change first, but it's something I'm happy to add

This should be released within the next few hours

arrays

๐ŸŽ‰ This issue has been resolved in version ts-quickfixes-plugin-v1.6.1 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€