const error = Symbol();
type CheckSubKey<K, SK extends string> = K extends `${string}${SK}${string}`
? SK
: SK & { [error]: '(!) Invalid subkey (!)' };
interface GetValue {
<T extends object>(obj: T): <K extends keyof T>(key: K) => T[K];
<T extends object, I extends boolean = false>(
obj: T,
isIncludeKey?: I,
): I extends true
? <K extends string>(key: CheckSubKey<keyof T, K>) => T[`${string}${K}${string}` & keyof T][]
: <K extends keyof T>(key: K) => T[K];
}
const getValue: GetValue =
(obj: object, isIncludeKey = false) =>
(key: unknown): unknown[] => {
if (!isIncludeKey) {
return obj[key as keyof typeof obj];
}
return Object.entries(obj).reduce((result: unknown[], pair: [key: string, value: unknown]) => {
const [pairKey, pairValue] = pair;
if (pairKey.includes(key as string)) {
return [...result, pairValue];
}
return result;
}, []);
};
const o = {
asd: 1 as const,
asdfg: '2' as const,
qweg: [3] as const,
};
const v1 = getValue(o)('asd'); // 1
const v2 = getValue(o, true)('a'); // (1 | "2")[]
const v3 = getValue(o, true)('f'); // "2"[]
const v4 = getValue(o, true)('g'); // ("2" | readonly [3])[]
const v5 = getValue(o, true)('uuu'); // error
type JoinKeys<T extends object> = JoinKeysDeep<T> extends infer J extends Record<
string,
unknown
>
? { [Key in J extends J ? keyof J : never]: J extends J ? J[Key] : never }
: never;
type JoinKeysDeep<
T extends object,
K extends keyof T = JoinKeysDeep.GetKeys<T> & keyof T,
> = K extends K & (string | number)
? T[K] extends object
? JoinKeysDeep<T[K]> extends infer J extends Record<string, unknown>
? J extends J
? Record<`${K | '*'}.${keyof J & (string | number)}`, J[keyof J]>
: never
: never
: Record<K | '*', T[K]>
: never;
namespace JoinKeysDeep {
export type GetKeys<T> = T extends unknown[]
? keyof T &
(number extends T['length']
? // array
number
: // tuple
`${number}`)
: keyof T;
}
inlayHints
. https://code.visualstudio.com/docs/typescript/type... type ButtonType = 'button' | 'a';
interface ButtonTypeProps extends Record<ButtonType, object> {
button: {};
a: { href: string };
}
type CommonProps = {
children: React.ReactNode;
variant?: 'plain' | 'tonal' | 'filled' | 'outlined';
justify?: 'start' | 'center' | 'end';
};
type Props<T extends ButtonType> = ButtonTypeProps[T] & CommonProps & ComponentPropsWithoutRef<T>;
type ButtonComponent<T extends ButtonType = ButtonType> = UnionToIntersection<
T extends T ? ForwardRefExoticComponent<Props<T> & RefAttributes<ComponentRef<T>>> : never
>;
const isLinkProps = (props: Props<ButtonType>): props is Props<'a'> => 'href' in props;
const Button = forwardRef(
<T extends ButtonType = ButtonType>(props: Props<T>, ref: ForwardedRef<ComponentRef<T>>) => {
if (isLinkProps(props)) {
return (
<a ref={ref as ForwardedRef<ComponentRef<'a'>>} href={props.href} className="button">
{props.children}
</a>
);
}
return (
<button ref={ref as ForwardedRef<ComponentRef<'button'>>} type="button" className="button">
{props.children}
</button>
);
},
) as ButtonComponent;
Button.displayName = 'Button';
await waitFor(() => screen.getByText('Projects'), {
timeout: 3000
})
type ExtractValuesFromSubKeys<T extends object, SK extends string, K extends keyof T & string = keyof T & string> = K extends `${string}${SK}${string}` ? T[K] : never
function getValue<T extends object>(obj: T, isIncludeKey?: false): <K extends keyof T>(key: K) => T[K]
function getValue<T extends object>(obj: T, isIncludeKey: true): <SK extends string>(subKey: SK) => ExtractValuesFromSubKeys<T, SK>[]
function getValue<T extends object>(obj: T, isIncludeKey?: boolean) {
return (keyOrSubKey: string) => {
if (!isIncludeKey) {
const key = keyOrSubKey as keyof T
return obj[key];
}
const subKey = keyOrSubKey
return Object.keys(obj).filter(key => key.includes(subKey)).map(key => obj[key as keyof T])
}
}
const getter = getValue({ aaa: 1, bbb: '2', ccc: [3], abc: null }, true);
const ttt1 = getter("a") // (number | null)[]
const ttt2 = getter("bb") // string[]
const ttt3 = getter("ccc") // number[][]
const ttt4 = getter("unknown") // never[]
type QQQ = {
a?: never;
b: string;
c: string;
};
type WWW = {
a: string;
b: string;
d: string;
};
type ForwardedRefWithOverride<T extends [object, HTMLElement]> = UnionToIntersection<
T extends T ? ForwardRefExoticComponent<PropsWithoutRef<T[0]> & RefAttributes<T[1]>> : never
>;
const forwardRefWithOverrides = <T extends [object, HTMLElement][]>(
render: ForwardRefRenderFunction<T[number][1], T[number][0]>,
) => {
return forwardRef(render) as ForwardedRefWithOverride<T[number]>;
};
export const Ccc = forwardRefWithOverrides<[[QQQ, HTMLButtonElement], [WWW, HTMLLinkElement]]>((props, ref) => {
if (typeof props.a === 'string') {
const { a, b, d } = props;
return (
<>
<link ref={ref as ForwardedRef<HTMLLinkElement>} />
{a}
{b}
{d}
</>
);
}
{
const { a, b, c } = props;
return (
<button ref={ref as ForwardedRef<HTMLButtonElement>} type="button">
{a}
{b}
{c}
</button>
);
}
});
Ccc.displayName = 'Ccc';
export const Vvv = () => {
const ref1 = useRef<HTMLButtonElement>(null);
const ref2 = useRef<HTMLLinkElement>(null);
return (
<>
<Ccc ref={ref1} b="asd" c="asd" />
<Ccc ref={ref2} a="asd" b="asd" d="asd" />
{/* @ts-expect-error - c missed */}
<Ccc b="asd" d="asd" />
{/* @ts-expect-error - d missed, extra c */}
<Ccc a="asd" b="asd" c="asd" />
{/* @ts-expect-error - extra c */}
<Ccc a="asd" b="asd" c="asd" d="asd" />
{/* @ts-expect-error - wrong ref */}
<Ccc ref={ref2} b="asd" c="asd" />
</>
);
};
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T;
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
normalizeRules.reduce((acc: string, rule) => acc.replaceAll(rule, ""), input);
// or
normalizeRules.reduce<string>((acc, rule) => acc.replaceAll(rule, ""), input);
type OverloadProps<TOverload> = Pick<TOverload, keyof TOverload>;
type OverloadUnionRecursive<TOverload, TPartialOverload = unknown> = TOverload extends (
...args: infer TArgs
) => infer TReturn
? // Prevent infinite recursion by stopping recursion when TPartialOverload
// has accumulated all of the TOverload signatures.
TPartialOverload extends TOverload
? never
:
| OverloadUnionRecursive<
TPartialOverload & TOverload,
TPartialOverload & ((...args: TArgs) => TReturn) & OverloadProps<TOverload>
>
| ((...args: TArgs) => TReturn)
: never;
type OverloadUnion<TOverload extends (...args: any[]) => any> = Exclude<
OverloadUnionRecursive<
// The "() => never" signature must be hoisted to the "front" of the
// intersection, for two reasons: a) because recursion stops when it is
// encountered, and b) it seems to prevent the collapse of subsequent
// "compatible" signatures (eg. "() => void" into "(a?: 1) => void"),
// which gives a direct conversion to a union.
(() => never) & TOverload
>,
TOverload extends () => never ? never : () => never
>;
// Inferring a union of parameter tuples or return types is now possible.
type OverloadParameters<T extends (...args: any[]) => any> = Parameters<OverloadUnion<T>>;
type OverloadReturnType<T extends (...args: any[]) => any> = ReturnType<OverloadUnion<T>>;
__tests__
на том же уровне, что и тестируемый файл. Локальные моки там же, глобальные для пекеджей согласно документации библиотеки для тестирования, а свои глобальные уже смотреть надо, в фсд можно в шейрд запихнуть.The setting you are looking for is "compact folders". You can get there by going to: File → Preferences → Settings → explorer.compactFolders and then unchecking the box.
Параметр, который вы ищете, это «компактные папки». Вы можете попасть туда, выбрав: Файл → Настройки → Настройки → explorer.compactFolders, а затем сняв флажок.