-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Bug: Argument of string literals list can't be used to construct template literal type #43198
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
Without an type Join<T extends readonly unknown[], D extends string> =
T extends readonly [] ? '' :
T extends readonly [string | number | boolean | bigint] ? T[0] :
T extends readonly [string | number | boolean | bigint, ...infer U] ? `${T[0]}${D}${Join<U, D>}` :
string;
class Foo<T> {
// Capture type T so we can narrow to `K extends keyof T`
constructor(private obj: T) {}
// `K extends string[]` doesn't work either
combineKeys<K extends readonly (keyof T & string)[]>(keys: K) {
return keys.join('_') as Join<K, '_'>;
}
}
// works
const key = new Foo({ "a": 0, "b": 1 }).combineKeys(["a", "b"] as const); |
@RyanCavanaugh Ah great, the one new feature I haven't gotten up to speed on 😅. Is this something that can only be at the call-site, or can we capture the const-ness of the argument in the argument's generic type (or something else like internally cast as The example in this issue is intentionally trivial for replicating the issue, but requiring library consumers to pass |
Very similar to an issue I just ran into. Was very much looking forward to typing tuple members after a rest type, but in practice it's not as elegant as I'd hoped.
|
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
An argument of string literals list can't be used to construct a new template literal type.
This could be a mistake on my part, but I've scoured the issues list and typed template literal PRs/docs and haven't seen anything that should preclude this. Small repro below.
🔎 Search Terms
keyof, typed template literals
🕗 Version & Regression Information
4.1+
⏯ Playground Link
💻 Code
🙁 Actual behavior
key
is typestring
.🙂 Expected behavior
key
should be type'a_b'
The text was updated successfully, but these errors were encountered: