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

typeof returns the template instead of the actual string #46259

Closed
esthedebeste opened this issue Oct 7, 2021 · 4 comments
Closed

typeof returns the template instead of the actual string #46259

esthedebeste opened this issue Oct 7, 2021 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@esthedebeste
Copy link

esthedebeste commented Oct 7, 2021

Bug Report

πŸ”Ž Search Terms

typeof key, typeof template, template key

⏯ Playground Link

`number_${string}` cannot be assigned to "number_three", even though that will be the value.

πŸ’» Code

type Numbers = {
	[numberToParse: `number_${string}`]: (num: typeof numberToParse) => number;
};
const a: Numbers = {
	// Because of the `Numbers` type, typescript
	// should know that `num` is "number_three".
	// Instead, `num` is said to be a template,
	// So number_three fails to compile.
	number_three(num: "number_three") {
		return 3;
	},
};

A shorter example:

function hi(name: string): `Hi, ${typeof name}!`;
hi("World"); // Expected: "Hi, World!", got `Hi, ${string}!`

Though this could simply use generics

function hi<U extends string>(name: U): `Hi, ${U}!`;
hi("World"); // βœ”οΈ "Hi, World!"

πŸ™ Actual behavior

The num variable is annotated "number_${string}"

πŸ™‚ Expected behavior

The num variable is annotated "number_three"

@esthedebeste esthedebeste changed the title typeof returns the template instead of the actual string. typeof returns the template instead of the actual string Oct 7, 2021
@MartinJohns
Copy link
Contributor

[numberToParse: number_${string}]: (num: typeof numberToParse) => number;

But the typeof numberToParse is always number_${string}. It doesn't change.

@esthedebeste
Copy link
Author

then how can I access the key as a string, aside from using typeof key?

@DanielRosenwasser
Copy link
Member

You would need a way to encode type parameters on index signatures, or some similar feature, which is discussed a bit over at #22509. Right now, we don't have any way of saying that an index signature is generic.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 11, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants