Skip to content

Record property string literal and <typeof classType["name"]> not working #47203

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

Closed
andtii opened this issue Dec 20, 2021 · 5 comments
Closed

Comments

@andtii
Copy link

andtii commented Dec 20, 2021

Bug Report

πŸ”Ž Search Terms

typeof ["name"]
get class name

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground Link

πŸ’» Code

type Class<T> = {
  new(...args: any[]): T;
}

export function useInjectables<TClass1, TClass2>(classType: Class<TClass1>, classType2: Class<TClass2>): Record<`${Uncapitalize<typeof classType["name"]>}`, TClass1> & Record<`${Uncapitalize<typeof classType2["name"]>}`, TClass2>
export function useInjectables<TClass>(classType: Class<TClass>): Record<`${Uncapitalize<typeof classType["name"]>}`, TClass>
export function useInjectables<TClass>(...args: Array<Class<TClass>>): { [key: string]: any } {
  //TODO implement when typing works
  return { test: null };
}

class One {
  title = "Test";
}

class Two {
  something = "Else";
  more = "hepp";
}

const { one, two } = useInjectables(One, Two);

//Expect to only have title prop
one.title;
one.something

//Expect to only have something prop
two.something;
two.title

πŸ™ Actual behavior

The Record<${Uncapitalize<typeof classType["name"]>}, TClass1> does not set the propertyName to be the name of the classType.
and both objects is merged into one. If i change it to hardcoded value like this: Record<${Uncapitalize<'one'>} it will work.

πŸ™‚ Expected behavior

The propertyNames should automatically be inferred by <typeof classType["name"]>

@MartinJohns
Copy link
Contributor

Your issue can be simplified to:

type Class<T> = { new(...args: any[]): T; }

class One { title = "Test"; }

type TName = Class<One>["name"]
// Expected "One", is string.

And then it's a duplicate of #43325, which was closed by the author in favour of #1579, which will most likely not happen in TypeScript.

There's also #32527, which is basically the same as well.

@fatcerberus
Copy link

I think this is because typeof Class["name"] is just string?

@andtii
Copy link
Author

andtii commented Dec 20, 2021

Okey, thnx for the explanation. Is there another way to get the incoming types so it can be used as the property name on the returning object? To achieve the goal here or something similar?

@MartinJohns
Copy link
Contributor

Only by passing a name manually.

@xiBread
Copy link

xiBread commented Dec 20, 2021

If you don't mind using plugins then I suggest https://github.com/dsherret/ts-nameof

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants