Extract types from object literals instead of using the type checker.#60540
Extract types from object literals instead of using the type checker.#60540dragomirtitian wants to merge 1 commit intomicrosoft:mainfrom
Conversation
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
| x: string; | ||
| get x(): string; | ||
| /** my awesome setter (second in source order) */ | ||
| set x(a: string); |
There was a problem hiding this comment.
This is a notable behavior change.
I personally believe that we should stop synthesizing props when getters were written, but this also makes me wonder what's going on below with obj4.
There was a problem hiding this comment.
If only one accessor is defined, this code does still use the previous behavior of creating a property signature.
| //// [declarationEmitPropertyNumericStringKey.d.ts] | ||
| declare const STATUS: { | ||
| readonly "404": "not found"; | ||
| readonly ["404"]: "not found"; |
There was a problem hiding this comment.
This is a change, but is probably fine.
| } | ||
| export declare const DOMMouseButton: { | ||
| '-1': MouseButton; | ||
| "-1": MouseButton; |
There was a problem hiding this comment.
The quote style in the original code is single; if we're copying this, shouldn't the quote have stayed?
There was a problem hiding this comment.
This is a bit of a strange one, because sometimes TS normalizes strings that come from values and sometimes it does not. If you assign a string to a const, TS never preserved those as written and replaced the string delimiters. I preserved this behavior, and it is now applied to string property names:
const x = '1' // We get "1" in d.ts
const y = { '1': ''} // We get '1' in d.ts.
There was a problem hiding this comment.
I guess I don't know why this is a desirable change, if the behavior differs depending on where the string is used...
There was a problem hiding this comment.
I opened a PR that shows what the impact of preserving delimiters more widely would be: #60729
| declare namespace m1 { | ||
| var n: { | ||
| 'foo bar': number; | ||
| "foo bar": number; |
This PR brings TS emit closer to what an external tool could emit without type information by extracting object type directly from their source object literal where possible.