-
Notifications
You must be signed in to change notification settings - Fork 12.8k
['a' | 'b']
expect to receive ['a']
#55225
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
Why do you expect this behaviour? What's your reasoning and thought process? The return argument of your function is just |
I want result is |
input |
export type DeepNamePath<T = any> = T extends Record<string, any>
? {
- [P in keyof T]: [P] | DeepNamePath<T[P]>;
+ [P in keyof T]: [P] | [...DeepNamePath<T[P]>];
}[keyof T]
: never;
If I write this way, I can get |
Your second argument gets widened to TS doesn't recognize that your For similar reasons this one work: export type DeepNamePath<T = any> = T extends Record<string, any>
? {
[P in keyof T]: readonly [P] | DeepNamePath<T[P]>;
}[keyof T]
: never;
type ddd<T = any> = DeepNamePath<T>;
function func<T = any, const T1 extends ddd<T> = ddd<T>>(data: T, params: T1) {
console.log('data', data);
return params;
}
export const d = func({ a: '', b: '' }, ['a']); This one doesn't really recognize the tuple-like base constraint but it introduces a |
I thought of const, but I couldn't write it at the time. |
Since this hasn't been said explicitly yet: this isn't a bug in TypeScript so this isn't really the right place for this discussion. Stack Overflow or the TS Discord would be more appropriate venues. @crazyair, could you please close the issue to free up the TS team's time? Once it's closed I imagine anyone who's still interested could continue discussing here. edit: Thanks! 🙏 |
export type DeepNamePath<T = any> = T extends Record<string, any>
? {
[P in keyof T]: readonly [P] | DeepNamePath<T[P]>;
}[keyof T]
: never;
type ddd<T = any> = DeepNamePath<T>;
function func<T = any, const T1 extends ddd<T> = ddd<T>>(data: T, params: T1) {
console.log('data', data);
return params;
}
// export const d = func({ a: '', b: '' }, ['a']);
export interface ColumnType<
RecordType = any,
T1 extends DeepNamePath<RecordType> = DeepNamePath<RecordType>,
> {
dataIndex?: T1;
render?: (value: T1) => any;
}
export interface DemoProps<T = any> {
data: readonly T[];
columns: ColumnType<T, DeepNamePath<T>>[];
}
export const result: DemoProps<{ a: number; b: string }> = {
data: [{ a: 1, b: '' }],
columns: [{ dataIndex: ['a'], render: value => value }],
};
|
-export interface ColumnType<RecordType = any, T1 extends DeepNamePath<RecordType> = any> {
+export interface ColumnType<RecordType = any, const T1 extends DeepNamePath<RecordType> = any> {
dataIndex?: T1;
render?: (value: T1) => any;
}
|
To make it work you need to be in some inference context and that means that you need a function call to make it work (and annotate its type params as export type DeepNamePath<T = any> = T extends Record<string, any>
? {
[P in keyof T]: readonly [P] | readonly [...DeepNamePath<T[P]>];
}[keyof T]
: never;
declare function make<T, const T2 extends readonly DeepNamePath<T>[]>(arg: {
data: T[];
columns: {
[K in keyof T2]: {
dataIndex: T2[K];
render: (value: T2[K]) => void;
};
};
}): void;
export const result = make({
data: [{ a: 1, b: "" }],
columns: [{ dataIndex: ["a"], render: (value) => value }],
}); but it doesn't because TS doesn't currently handle context-sensitive functions in reverse-mapped types that well. It's something I want to improve in #54029 |
I need const demo: DemoProps<xx> = xxx |
If declare function func<TData = any, const T1 extends keyof TData = any>(data: {
data: TData[];
list: {
key?: T1;
render?: (value: T1) => any;
}[];
}): any;
func({
data: [{ a: 1, b: '2' }],
list: [
{ key: 'a', render: value => value },
{ key: 'b', render: value => value },
],
}); |
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
DeepNamePath<T>
result is equal to['a'] | ['b']
, but result different🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
['a']
🙂 Expected behavior
['a' , 'b']
The text was updated successfully, but these errors were encountered: