Open ericwooley opened 2 years ago
Just to make sure I understood this properly, the idea is that the user would select User | Book | Library
and be offered the option to generate the interface, and that interface would contain every property which exist in all of the constituents of the union?
Just to make sure I understood this properly, the idea is that the user would select User | Book | Library and be offered the option to generate the interface, and that interface would contain every property which exist in all of the constituents of the union?
Correct, except I'm not exactly sure if we're on the same page about this part:
every property which exist in all of the constituents of the union?
It wouldn't be every property in the constituents of the union, just the properties which are used by this method/function
Hmm, interesting. What you're describing isn't very far off from the existing "Infer parameter types from usage" refactor that appears when a parameter doesn't have a type annotation.
Hmm, interesting. What you're describing isn't very far off from the existing "Infer parameter types from usage" refactor that appears when a parameter doesn't have a type annotation.
Hmm, i haven't seen that? Under what circumstances does that appear. I do not see it when running refactor on an argument with implicit any. I just see these options in vs code with typescript 4.5.5
@ericwooley
Result is:
function fooey(foo: { x: number; y: string; })
{
const x: number = foo.x;
const y: string = foo.y;
}
@fatcerberus
Oh nice, thanks! I never noticed that, I guess I just always write my types out before I start writing the body π€·
I tried playing with that in the TS playground to see if it would be sufficient help in the User
example, and the best I could get it to infer was any[]
. Which isn't surprising, but I wanted to be sure I wasn't missing things
@RyanCavanaugh Is there anything I need to add to get past the "Awaiting More Feedback" label?
@ericwooley no, AMF is sort of the default state for suggestions that don't need design work but don't currently meet the bar for adding to the codebase
Suggestion
π Search Terms
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
A new refactor feature which creates a minimal interface based on usage for an argument.
π Motivating Example
Imagine we have created an interface, User.
When we started with our project, we created some super fancy rendering table which shows a list of users by name, and when they were last_updated, and when they were created.
Then we decide to add books to our app.
We want the same functionality for rendering books as we have for users. Luckily,
renderTable
is pretty re-usable. So we just add our interface to types for data:Then, we add Libraries, and repeat
We could keep adding to this list, but in order to avoid infinite joins, we might want to refactor this, so that the data type only specifies what it actually needs. So we create a generic interface, maybe called TableRenderable, which has all the properties we need to make the data renderable.
Nice, now we have a minimal interface that this table needs to be able to render the data.
That was easy enough, but imagine if table weren't so simple, maybe it called half a dozen other functions, which all use the User interface, and had a few more than 3 properties.
In order to refactor, you would need to first go into each function, and create the minimal interface for each function, then on the renderTable function, create the minimal interface
TableRenderable
. This still isn't that bad, but it is a bit time consuming.Typescript can help by implementing a refactor feature
Generate Minimal Interface
. Typescript already has all the information needed in the function to know which properties are being used by this function, and what types the are. We just need to combine all that information into a new interface. Tedious for human beings, simple for a type system.Now with the new feature, you can start with each function
renderTable
calls, replace theUser
type with a generated minimal interface. Then finally, in the renderTable function, create theTableRenderable
interface.Instead of 10+ minutes in many of my real use cases, decoupling functions from the User interface could take a minute or 2.
π» Use Cases
Decoupling interfaces from functions much more quickly.
Decoupling in this manner is manual, as far as I am aware, which is ok, but it's something that a computer could do just as easily.