-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Mapped keys do not preserve JSDoc documentation #50715
Comments
This seems like an extension of #47933; if you write code like (Playground Link): type someType = {
/**
* some text to be displayed
*/
[key:string]: string;
};
declare const test: someType;
someType.anything; Hovering over |
FYI @danay1999 |
@jakebailey Thank you for the heads up. It seems the main issue is that it doesn't conserve the JSDoc from the original keys, and therefore it has nothing to show. I am not sure if it would count as an extension of #47933 |
What I cited was just a brief example. But I haven't come across types working as they should (as I reported) in real use cases. |
I know it's hard to project this, but as far as the primary problem at least is there any light on when it might be resolved? |
I'd like to have this, too. Having dug into the code, it looks like this is mostly an intentional design decision. I'm interested in revisiting that, if it is. That said, I think there are a few potential issues with just blithely passing along the documentation. The most significant one is simply that the documentation may well not be correct for the mapped properties. Consider a version of your example above using setters: type Setters<Type> = {
[Property in keyof Type as `set${Capitalize<string & Property>}`]: (value: Type[Property]) => void
}; The documentation "Person's name." doesn't really fit the method But since there are some solid use cases for being able to bring the documentation along, I wonder if it might be worthwhile to add some kind of template-literal-like syntax to the JSDoc to enable it explicitly. Consider something like type Setters<Type> = {
/** Set ${Uncapitalize<docof Property>} */
[Property in keyof Type as `set${Capitalize<string & Property>}`]: (
/** New value for ${Uncapitalize<docof Property>} */
value: Type[Property]
) => void
};
interface Person {
/** Person's name */
name: string;
/** Person's age */
age: number;
}
const me: Setters<Person> = {
// π¬ Documentation: "Set person's name"
setName: (value) => {},
// π¬ Documentation: "Set person's age"
setAge: (value) => {}
};
// π¬ Documentation on parameter: "New value for person's name"
me.setName("Jake Carter")
// π¬ Documentation on parameter: "New value for person's age"
me.setAge(35) The other way to go would be the proposal for full on imperative types. I'm not sure if this idea is stronger or weaker for being more constrained than that. |
your observations are very valid, @Peeja. I honestly don't know what the best way is, but I really hope that something can be done to allow for this more "dynamic" control at the documentation level on mapped types. |
I noticed that interface Person {
/** Person's name */
name: string;
/** Person's age */
age: number;
}
type PickName = Pick<Person, 'name'>;
const pickName: PickName = {
name: 'alice', // <-- has JSDoc
};
interface ExplicitName {
name: Person['name'];
}
const explicitName: ExplicitName = {
name: 'bob', // <-- no JSDoc
}; JSDoc seems to get preserved by a "homomorphic mapped type" but not by anything else. |
We can refactor once microsoft/TypeScript#50715 has been resolved.
* feat: Expose `Gaxios` instance and default * feat: Unify Base `AuthClient` Options * docs: clean up documentation * chore: Discourage external use via `@internal` * refactor: minor * refactor: clean up `transporter` interface, options, and docs * Merge branch 'main' of github.com:googleapis/google-auth-library-nodejs into authclient-enhancements * Merge branch 'main' of github.com:googleapis/google-auth-library-nodejs into authclient-enhancements * test: Init tests for `AuthClient` * fix: Use entire `JWT` to create accurate `createScoped` * chore: update `typescript` in fixtures * test: Use Sinon Fake Timer to Avoid Flaky Time Issue (#1667) * test: Use Sinon Fake Timer to Avoid Flaky Time Issue * chore: change order for clarity * docs(sample): Improve `keepAlive` sample with `transporterOptions` * docs: Docs-deprecate `additionalOptions` * refactor: un-alias `getOriginalOrCamel` * chore: remove confusing duplicate interface * docs: nit * docs: Improve camelCased option documentation We can refactor once microsoft/TypeScript#50715 has been resolved. * refactor: Unify `OriginalAndCamel` & `SnakeToCamelObject` + make recursive * docs: Add issue tracking link * refactor: Replace `getOriginalOrCamel` with a cleaner API * feat: Allow optional `obj` * refactor: re-add `SnakeToCamelObject` * refactor: dedupe interface for now * feat: Add camelCase options for `IdentityPoolClient`
Realized it would make sense if my automatic JSX types could have their JSDoc annotation descriptions from the base `HTMLElement`-kind of interface definitions they are already based on, but that doesn't appear to be possible for mapped types unfortunately. https://jsdoc.app/tags-borrows https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#link (this is an alternative, where `@borrows` won't work [i'm not sure if it works in TS anyways, but yeah], `@link` is a nice alternative if that eventually does work :) ) https://stackoverflow.com/questions/70766995/how-can-i-link-something-that-isnt-imported (not related specifically, but neat otherwise :D ) microsoft/TypeScript#50715 The next thing I thought of is that an option to get automatic types for JSX element definitions based on `HTMLElement`-based interfaces more narrowed than it currently is. Right now it lets you assign attributes that are readonly, and also assign to element methods, which isn't ideal. It could just be 'ignored', but this defeats the purpose of TypeScript, which should prevent accidentally overriding that kind of thing. Most JSX types implementations declare their own types for the element creation, I think that's a big extra though, in terms at least with what and why I'm trying to work on this library. Since it's creating standard elements, I think it makes a lot of sense to derive the JSX types from those dynamically, hence that will also allow you to get automatic type definitions for Web Component classes, and it will work without the implementor of that component to provide their own JSX types for this project. The key is that it works in your favor, you don't need to make it work with this tool. (Inspired behind the TypeScript origin story; https://www.youtube.com/watch?v=U6s2pdxebSo&t=2200s) Since the types for the `HTMLElement` prototype tree are essentially a static list of members (the only difference being new features added down the line), it seems feasible to omit specific keys which shouldn't be assignable/visible in JSX, simply by removing keys that are present in parent prototypes of the base `HTMLElement` class. I went up the prototype tree to get `HTMLElement > Element > Node > EventTarget > Object`. Now I can go through these values to deduce which ones can be removed from the JSX types, hence allowing only the necessary ones to be accessible from the user's point of view when assigning props and such. https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys
Adding to this, type Text= {
otherProperty: string,
/**
* @deprecated <- This is also not working
*/
[key: string]: unknown;
} |
I use renaming of mapped properties quite often to prefix properties for React components to be passed down to sub components, it really sucks having the documentation for the original source properties essentially disappearing into the ether due to the rewriting. It'd be great if we could get some kind of hinting for documentation on generated properties to pull in the source documentation. |
Chiming in here. We have a typegen system that gathers all the database tables, and each table's columns, and generates type files. Each column has a JSDoc with some useful tidbits, like the actual postgres table column type ( For example: /**
* Column: date_activated
*
* @description The SQL Comment on the date_activated column in the db
* @type timestamp without time zone
* @default "('now'::text)::timestamp without time zone"
*/
date_activated: Date | null; We also have a utility that turns that table object into a camelCased version of itself for multiple uses. Without the camel casing, JSDocs come through exactly as expected, but once we do: [K in keyof Obj as CamelCase<K>]: Obj[K]; we lose the JSDocs. Would be mighty cool to be able to keep the original JSDocs! |
Bug Report
π Search Terms
mapping types with docs; preserve docs on mapped types
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
All relevant code can be found in the example links.
Why doesn't TypeScript/Vscode preserve documentation / JSDoc for mapped types?
Using this example that appears in the TypeScript documentation itself, which you can quickly reproduce in Vscode locally, you can see the problem.
It is expected that when remapping the keys of a type object, the documentation would remain what was defined in the original/previous keys, but this does not happen. Nothing is preserved.
This sucks in the development context, because it requires us to have to repeat the documentation of a property or method, for example, in multiple places, thousands of times. This can lead to consistency issues as the code grows and loses the point of automating things.
Another directly related problem is in Index Signatures. Even if documentation is added, nothing is preserved. This also includes Union Types. An example can be seen here.
π Actual behavior
JSDoc documentation is not preserved in mapped keys.
π Expected behavior
Mapped keys must have JSDoc documentation of their original/previous names.
The text was updated successfully, but these errors were encountered: