type-fest
type-fest copied to clipboard
ConditionalKeys didn't work fine as expected
import {ConditionalKeys} from 'type-fest';
interface Example {
a: string;
b: string | number;
c?: string;
d: {};
}
type StringKeysOnly = ConditionalKeys<Example, string>;
Expect to be 'a' but get 'a' | 'c' actually
export type ConditionalKeys<Base, Condition> = NonNullable<
// Wrap in `NonNullable` to strip away the `undefined` type from the produced union.
{
// Map through all the keys of the given base type.
[Key in keyof Base]:
// Pick only keys with types extending the given `Condition` type.
Base[Key] extends Condition
// Retain this key since the condition passes.
? Key
// Discard this key since the condition fails.
: never;
// Convert the produced object into a union type of the keys which passed the conditional test.
}[keyof Base]
>;
ts version: 4.0.3
why...

Upvote & Fund
- We're using Polar.sh so you can upvote and help fund this issue.
- The funding will be given to active contributors.
- Thank you in advance for helping prioritize & fund our backlog.
Is your project configured for strict null checking?
Is your project configured for strict null checking?
... The project was forked from the repo. There was no other configuration. Is it still possible to be configured somewhere?
This project uses strict null checking. I have confirmed the findings.
This is really weird.
interface Example {
a: string;
b?: string | number;
c?: string;
d: Record<string, unknown>;
}
declare const exampleConditionalKeys: ConditionalKeys<Example, string>;
expectType<'a'>(exampleConditionalKeys);
Correctly results in 'a', but going through a type does not:
interface Example {
a: string;
b: string | number;
c?: string;
d: {};
}
type S = ConditionalKeys<Example, string>;
declare const exampleConditionalKeys: S;
expectType<'a'>(exampleConditionalKeys);
// @ifiokjr @kainiedziela Any ideas?
This project uses strict null checking. I have confirmed the findings.
There has no "strictNullChecks": true, in tsconfig.json and "extends": "@sindresorhus/tsconfig" either. How can I confirm that.
@sindresorhus/tsconfig has strict: true which sets strictNullChecks: true.
Thanks, got it.