镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

infer on setter argument gives unknown #47146

Closed

Description

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

Activity

  1. RyanCavanaugh commented on Dec 14, 2021

    @RyanCavanaugh
    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.

  2. Galphimbl commented on Dec 14, 2021

    @Galphimbl
    Author

    Ryan Cavanaugh (@RyanCavanaugh) i try to get setter type, is there any solution ?

  3. fatcerberus commented on Dec 14, 2021

    @fatcerberus

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

  4. a-tarasyuk commented on Dec 14, 2021

    @a-tarasyuk
    Contributor

    Ryan Cavanaugh (@RyanCavanaugh) Should TS throw the following error?

    A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
  5. RyanCavanaugh commented on Dec 14, 2021

    @RyanCavanaugh
    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
  6. Galphimbl commented on Dec 14, 2021

    @Galphimbl
    Author

    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?

  7. RyanCavanaugh commented on Dec 15, 2021

    @RyanCavanaugh
    Member

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

  8. RyanCavanaugh commented on Dec 15, 2021

    @RyanCavanaugh
    Member

    Wow, this is super broken...

    type OH_NO = {
      get [K in WAT](): string
    };
  9. added a commit that references this issue on Dec 15, 2021
    53170d9
  10. MartinJohns commented on Dec 16, 2021

    @MartinJohns
    Contributor

    Oleksandr Tkachov (@Galphimbl) You might want to follow #43729.

  11. added a commit that references this issue on Feb 1, 2022
    2172e19
  12. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions