Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to insert interface template into function parameters? #1443

Open
L1Q opened this issue Aug 15, 2018 · 3 comments
Open

Is there a way to insert interface template into function parameters? #1443

L1Q opened this issue Aug 15, 2018 · 3 comments

Comments

@L1Q
Copy link

L1Q commented Aug 15, 2018

Some context

I made a function that takes an interface with 5 properties as an argument.

interface MyInterface {
    readonly myProperty : string
    readonly anotherProperty: number
    optionalProperty1?: number[]
    optionalProperty2?: number
    // I can have some comments to describe this property in great detail
    lastProperty: string
}

function interfaceConsumer(myVariable: MyInterface): string {

    /* some code here */

    return ""
}

TS knows everything about that interface, it shows hints about expected properties and would warn me when in function arguments I create an object not compatible with the interface.
TS shows hints for interface properties
TS warns when object does not conform to the interface

So can I have a way to autocomplete that interface in function parameters?
I could copy-paste interface declaration, sure, but it's not the same and I would need to add commas, remove additional metadata or comments I might have there.

What would it look like?

Simulated experience with TS interface template autocompletion

interfaceConsumer({
     myProperty       : string
    ,anotherProperty  : number
    ,optionalProperty1: number[] // ?
    ,optionalProperty2: number // ?
    ,lastProperty     : string
})
  • Leading commas and alignment are my personal preferences of course.
  • Having types as initial "values" is quite crucial though: it's important to have there something invalid so that user does not leave a value unmodified by accident and having type information inserted could be handy IMO.
  • Dropping information about optional arguments doesn't look like a good idea, so I'd insert it back with trailing comment.

The question

Can I have such interface autocompletion experience now?
Tell me if there are plugins/hotkeys/workflows I'm not aware of that would let me have this experience.

@lierdakil
Copy link
Collaborator

Not sure this would be useful in general. Pretty sure it's possible to write a TS language service to do this. I'm personally not aware if any exist, and not very likely to spend my time on implementing it. But anyone willing to try their hand at this could start here

@apoorvakadam
Copy link

I am new to open-source and I was hoping to contribute here.

@lierdakil
Copy link
Collaborator

Hello. Contributions are always welcome, but this particular issue might be a bit too complicated to start with. Besides, it makes much more sense to implement this outside of Atom-TS, i.e. as a TypeScript language service plugin (https://github.com/Microsoft/TypeScript/wiki/Writing-a-Language-Service-Plugin).

I think it should be doable by hooking into getCompletionsAtPosition and adding a completion with all the fields there.

Getting all the fields might be a little tricky though, but in principle should boil down to getting the expected type at the current location (which is the trickiest part), then checking if it's an object, then iterating over its properties.

Perhaps an easier approach would be to hack into object literal completion (i.e. list of completions shown at {‸}), which should have all object properties and nothing else as completions (shown in the first screen-shot in OP)

As I said above, I'm not entirely sure how useful this would be in general. TypeScript already has object literal completions, which fulfil the same general purpose, albeit slightly differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants