Skip to content
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

infer on setter argument gives unknown #47146

Closed
Galphimbl opened this issue Dec 14, 2021 · 9 comments Β· Fixed by #47156
Closed

infer on setter argument gives unknown #47146

Galphimbl opened this issue Dec 14, 2021 · 9 comments Β· Fixed by #47156
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@Galphimbl
Copy link

Bug Report

πŸ”Ž Search Terms

setter, getter, infer

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Conditional Types

⏯ Playground Link

Playground Link

πŸ’» Code

interface Test1 {
    get prop(): string;
    set prop(v: string | number);
}
type SetterType<T, K extends keyof T> = T extends {set [P in K](v: infer U);} ? U : T[K];

declare const setter: SetterType<Test1, 'prop'>; // unknown 

πŸ™ Actual behavior

Hovering on setter gives you

const setter: unknown

πŸ™‚ Expected behavior

Hovering on setter gives you

const setter: string | number
@RyanCavanaugh
Copy link
Member

😲 set [P in K](v: U) was never supposed to be allowed syntax. I'm honestly shocked this doesn't crash; the syntax doesn't really work at all.

The "fix" here is to make this a syntax error, at least for now.

@RyanCavanaugh RyanCavanaugh self-assigned this Dec 14, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.6.0 milestone Dec 14, 2021
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 14, 2021
@Galphimbl
Copy link
Author

Galphimbl commented Dec 14, 2021

@RyanCavanaugh i try to get setter type, is there any solution ?

@fatcerberus
Copy link

TypeScript doesn't really distinguish between accessors and regular properties at the type level, so Test1['prop'] is about the best you can do.

@a-tarasyuk
Copy link
Contributor

@RyanCavanaugh Should TS throw the following error?

A computed property name must be of type 'string', 'number', 'symbol', or 'any'.

@RyanCavanaugh
Copy link
Member

The error should be that get/set can't be combined with mapped types.

type Foo1<K extends string> = {[P in K]: number};
// Should error
type Foo2<K extends string> = {get [P in K](): number};
//                             ^^^^^^^^^^^^
//  Cannot combine property get/set with mapped type syntax

type A = Foo1<"prop">;
type B = Foo2<"prop">;

type AP = A["prop"];
type BP = B["prop"];
// ^ never worked anyway

@Galphimbl
Copy link
Author

Galphimbl commented Dec 14, 2021

I tried to work with the changes added in release 4.3 ("Separate Write Types on Properties"), but there is no way to select only those setter keys that accept a specific data type.

will this feature be added in future releases?

@RyanCavanaugh
Copy link
Member

The future's a long time, but no this is not part of the scheduled roadmap

@RyanCavanaugh
Copy link
Member

Wow, this is super broken...

type OH_NO = {
  get [K in WAT](): string
};

RyanCavanaugh added a commit to RyanCavanaugh/TypeScript that referenced this issue Dec 15, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Dec 15, 2021
@MartinJohns
Copy link
Contributor

@Galphimbl You might want to follow #43729.

RyanCavanaugh added a commit that referenced this issue Feb 1, 2022
…rs (#47156)

* Add test that should fail

* Make it fail

Fixes #47146

* Baselines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants