Repository navigation
Provide completion lists for string literals #606
Description
Activity
+1
DanielRosenwasser commented
on Sep 5, 2014 MemberMore actions👍 would potentially like to take this on.
RyanCavanaugh commented
on Sep 5, 2014 MemberMore actionsTo consider: is this an editor-side feature or a language service feature?
DanielRosenwasser commented
on Sep 5, 2014 MemberMore actionsWhile it seems like it should be editor-side, I'd argue that's hard to generalize because of languages with multi-line string literals, and languages like TS/JS that have single-quote string literals. So to answer your question, I'm not actually sure.
The change is mainly on the services layer. we need to provide completions within strings, something we disallow today. there is a small change on the editor side to include strings in legal locations to ask for completions, but that should be trivial.
It's entirely possible this should be an general editor feature where a language service can opt into the set of tokens that would provide completions within strings. For example C# might want this feature but only using other string literals for completion suggestions. TypeScript and JavaScript language services may want a larger view of completion options in string literals that also considers other tokens like class method names (ex consider writing a handler for a Backbone route and then going back to the routes object literal and needing to add that method name in string form).
DanielRosenwasser commented
on Nov 11, 2015 MemberMore actionsNow that string literal types are a thing in the compiler, this will become more useful.
Additionally, much like object literal completions are informed by its contextual type, we should consider doing the same with string literal completions. For instance:
type T = "foo" | "bar"; let x: T = "$
At
$, I expect my completions to be only"foo"and"bar", regardless of any other identifiers in the file.- addedDomain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedUnit types including string literal types, numeric literal types, Boolean literals, null, undefined
on Mar 9, 2016 +1 please! Just to give you a user's point of view... I had assumed this would be included when string literals were released. But regardless would love to see this done when you guys are able!
👍 .
I hope this goes in soon as it will be quite useful.
Also JSDoc should be supported for the values:
/** Defines the permissible size measures. */ export type SizeRule = /** Indicates the smallest possible size */ 'tiny' | 'small' | ...;
Daniel Rosenwasser (@DanielRosenwasser) is this really fixed? #8125 looks like it fixes the enum issue Jon (@jbondc) mentioned in #606 (comment) but all the tests in that PR look enum related. This issue is really a broader suggestion around string literal values outside of the type system.
Reacted by Daniel RosenwasserDanielRosenwasser commented
on Apr 27, 2016 MemberMore actionsGitHub is not good at auto-closing if you just refer to fixing a comment (in that it totally shouldn't be doing that). Thanks for the heads up Dan. 😄
- addedFixedA PR has been merged for this issueA PR has been merged for this issue
on Jun 8, 2016 Mohamed Hegazy (@mhegazy) It doesn't look to me like #8428 addresses the example in the original suggestion. The compiler now gives type directed string literal suggestions but it doesn't just generally supply previously used string literals as completion options (like Sublime Text and other text editors do). Is that right?
- locked and limited conversation to collaborators
on Jun 18, 2018

Many JavaScript frameworks are heavily based on using string values to do many things, from getting and setting attributes via the property name in string form, to defining event handlers to handle browsing to particular URLs. TypeScript added specialized signatures in overloads as one means to help mitigate the pain associated with these types of APIs. Unfortunately for a number of patterns this is insufficient and not general enough. Rather than try to design a specific type system enhancement that could handle some or all of the many ways these frameworks use string literals we could try some simple tricks in Visual Studio to help out. Namely, when typing a string literal the editor should provide a completion list based on other string literals already used in your program. Some other tools already provide similar capabilities (ex Sublime Text).
Consider something like this in a Backbone View class:
At another point I may attempt to manually trigger one of these events:
This should be a relatively cheap solution to a common set of problems that we've always hoped TypeScript can improve on over JavaScript. If you did accidentally make a typo in one use of the literal this could also help you discover that when seeing 2 completion list items that are similar but slightly different.