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

Augment flag keys in ".flags" type definition #155

Closed
voxpelli opened this issue Jun 18, 2020 · 6 comments · Fixed by #218
Closed

Augment flag keys in ".flags" type definition #155

voxpelli opened this issue Jun 18, 2020 · 6 comments · Fixed by #218

Comments

@voxpelli
Copy link
Contributor

Unfortunately this doesn't seems doable yet, but it is something that is discussed here it seems: microsoft/TypeScript#12754

So currently .flags['foo-bar'] will have the same type as .unnormalizedFlags['foo-bar'], whereas only the latter should have that, the former should have it's key augmented into .flags.fooBar or at least have the errenous foo-bar key removed.

I believe none of that is possible right now, but wanted to open an issue and track that it would be preferable, as that can then eg. tie into the discussions within TypeScript itself.

@voxpelli
Copy link
Contributor Author

Correcting myself: .unnormalizedFlags shouldn't have kebabcased keys either.

@voxpelli
Copy link
Contributor Author

Closing this as this somewhat duplicates: #165 (comment)

Just a note, it may soon be possible to do something about this as the TypeScript 4.1 beta supports:

type Getters<T> = {
    [K in keyof T as `get${capitalize K}`]: () => T[K]
};

interface Person {
    name: string;
    age: number;
    location: string;
}

type LazyPerson = Getters<Person>;

If there would be a Split<"foo-bar", "-"> (they are switching to having capitalize K be Capitalize<K> in microsoft/TypeScript#40580) then maybe one could rewrite it as:

type CamelCase<K extends string> = `${Split<K, "-">[0]}${Capitalize<Split<K, "-">[1] | "">}

type CamelCasedProps<T> = {
    [K in keyof T as CamelCase<K>]: () => T[K]
};

interface KebabCased {
    "foo-bar": string;
    foo: number;
}

type CamelCased = CamelCasedProps<KebabCased>;

@voxpelli
Copy link
Contributor Author

Actually, it is already possible: microsoft/TypeScript#40710 (comment)

@sindresorhus
Copy link
Owner

@voxpelli Should we reopen this?

@voxpelli
Copy link
Contributor Author

voxpelli commented Nov 2, 2020

@sindresorhus Yeah, lets do it.

So, this would rely on sindresorhus/type-fest#134 I'm thinking.

  • it would require TypeScript 4.1, which I'm not really sure when/how one can reliably require? At least would have to wait for eg. VS Code to have shipped with it (and of course for TS 4.1 to itself have shipped)

Unless of course one can publish these new types in a way that works with older TS versions, but I doubt it

@voxpelli voxpelli reopened this Nov 2, 2020
@sindresorhus
Copy link
Owner

sindresorhus commented Nov 2, 2020

it would require TypeScript 4.1, which I'm not really sure when/how one can reliably require? At least would have to wait for eg. VS Code to have shipped with it (and of course for TS 4.1 to itself have shipped)

I'm ok with requiring TS 4.1 a couple of months after it has shipped. The benefits are worth it.

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

Successfully merging a pull request may close this issue.

2 participants