-
Notifications
You must be signed in to change notification settings - Fork 12.2k
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
[isolatedDeclarations][5.5] Type annotations needed for literals containing enum values #58394
Comments
I'm not 100% convinced this is safe. @dragomirtitian , thoughts? |
We generally don't look up any symbols beyond the current expression. So So we could do it in some cases. Our though process is that generally we should be consistent. It would be a bit strange for this to work only within a file. I think there is a case to be made that we could allow more of these in isolated declarations if there is demand for it. But I think this limitation should stay for v1 of this feature. |
That's fair. An idea I haven't put much thought into is to emit something like export enum Foo {
A = 1,
B = 2,
}
export declare const x: typeof Foo.A;
export declare const y: readonly [typeof Foo.A]; when the subject of I think a hard case might be something like: export enum Foo {
A = 1,
B = 2,
}
export const x = {
[Foo.A]: 1,
[Foo.B]: 2,
} as const; since you'd have to maybe emit something like export enum Foo {
A = 1,
B = 2,
}
export declare const x: {
readonly [K in typeof Foo.A]: 1;
} & {
readonly [K in typeof Foo.B]: 2;
} which I'd be ok with, but perhaps beyond the level of complexity you'd want to implement in tsc |
For computed properties we already allow this and error in TS if the type is not a late bindable computed property (ie it's not a property that can be a computed key and would have t be turned into a string index) For other positions, we did debate doing enum E { A = 1, }
let x = E.A;
const y = E.A // equivalent to typeof E.A
const s = Symbol();
const z = s // not equivalent to typeof s This is a class of inferences we called optimistic inferences. We did however rule them out for the time being as being too confusing to explain why this would fail sometimes. |
Subtle, didn't realize unique symbols had such unique behavior π |
@dragomirtitian I think I found a false negative, a case that should error but doesn't. // @filename: a.ts
export enum Foo {
A = 'a',
B = 'b',
}
// @filename: b.ts
import {Foo} from './a';
export const mapping = {
[Foo.A]: 1,
[Foo.B]: 2,
} |
π Search Terms
enum, isolated declarations
β Viability Checklist
β Suggestion
Enum values seem trivially inferrable if the enum is defined in the lexical scope, so isolated declarations shouldn't report them as errors.
π Motivating Example
Playground: https://tsplay.dev/w68Prm
π» Use Cases
Self-apparent
The text was updated successfully, but these errors were encountered: