-
Notifications
You must be signed in to change notification settings - Fork 12.8k
[isolatedDeclarations][5.5] Adding satisfies
to a const expression makes it require explicit type annotation
#58397
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
Comments
My impression was that |
Yep I've definitely seen cases where |
It is not unfortunately: const x = [function() { return "A"}] as const satisfies Array<() => "A">
const x = [function() { return "A"}] as const |
I wonder if the problem boils down to it not being possible to determine whether the export const x = [function() { return "A"}] as const satisfies Array<() => "A"> would be emitted as something like export declare const x: MakeSatisfy<readonly [() => string], [() => "A"]>; Are there cases where knowing only the |
Forgive me for pushing on this, as I'm sure folks are eager to treat this as an open and shut case. We have hundreds of instances of this pattern in our code base, the majority in which are cases where
So while we could migrate back to our old pattern (since duplicating the structure of the expression with an explicit type is the less desirable option) of doing a type test like function upcast<T>(value: T): void { return value }
export const foo = {
a: 42
} as const;
upcast<Record<string, number>>(foo); asking developers to write unidiomatic code with an abstruse explanation is an outcome I want to push against. [0] https://devblogs.microsoft.com/typescript/announcing-typescript-4-9/#satisfies |
|
@fatcerberus I get that, I'm not looking for a rationalization of the behavior |
There's a general class of "operations that non-trivially affect the actual type", including but not limited to:
Isolated declarations is designed to be syntax-only, meaning an external implementation (or TS internally) must always be able to detect when there's a problem or produce an equivalent annotation / initializer. If we do want to allow these things, then dts emit is going to have to gain some sort of syntax or builin type to describe these behaviors, but right now, they don't really exist. |
If the export const foo = {
a: 42
} as const;
foo satisfies Record<string, number>; |
While visually will take some getting used to, I suppose that's a good enough solution (thank you for taking the time to think of and provide one and not just mansplaining how things work). |
"Mansplaining" assumes bad faith; it isn't meant to be patronizing. Generally it's been my experience that when you tell people there's a design limitation preventing a desired behavior, they tend to say "well why can't you just do/not do this one thing instead", so it's easier just to explain how things interconnect upfront. Also why is "mansplaining" even a word |
π Search Terms
isolated declarations, satisfies, const
π Version & Regression Information
This changed in commit or PR #58201
β― Playground Link
https://tsplay.dev/wRbGEw
π» Code
π Actual behavior
Variable must have an explicit type annotation with --isolatedDeclarations.ts(9010)
π Expected behavior
No errors
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: