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

The union type cannot be determined using 'string []' to determine the type #55246

Closed
crazyair opened this issue Aug 3, 2023 · 11 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@crazyair
Copy link

crazyair commented Aug 3, 2023

⏯ Playground Link

Playground link with relevant code

💻 Code

type Type1 = { string?: 'a'; array?: readonly ['a']; getType?: (data: 'a') => any };
type Type2 = { string?: 'b'; array?: readonly ['b']; getType?: (data: 'b') => any };

export const type1: Type1 | Type2 = { string: 'a', getType: data => data }; // "a"

export const type2: Type1 | Type2 = { array: ['a'] as const, getType: data => data }; // any

🙁 Actual behavior

input ['a'], getType data isany

🙂 Expected behavior

input ['a'], getType data isa

@crazyair
Copy link
Author

crazyair commented Aug 3, 2023

I used const readonly, but still couldn't get it

@jcalz
Copy link
Contributor

jcalz commented Aug 3, 2023

Please properly fill out the Version & Regression Information section.

Tuples aren't treated as discriminants; this is essentially a duplicate of #48500.

@crazyair
Copy link
Author

crazyair commented Aug 3, 2023

Can we release the first level of derivation?

@crazyair
Copy link
Author

crazyair commented Aug 3, 2023

Add keywords

@jcalz
Copy link
Contributor

jcalz commented Aug 3, 2023

If I understand you correctly (and that's a big if, there seems to be a language barrier here), what you're asking for would require #18758

@jcalz
Copy link
Contributor

jcalz commented Aug 3, 2023

"This changed between versions 4.9 and 5.1" <-- this means that it was working as you desired in version 4.9 and is broken in version 5.1. Is that really what you mean? I'm going to disengage now because it seems like there's a communication problem and I don't know how to fix it. Good luck.

@crazyair
Copy link
Author

crazyair commented Aug 3, 2023

Can it only support (string | number)[]

@RyanCavanaugh
Copy link
Member

A reference-typed discriminant is difficult to reason about and would break a lot of assumptions that CFA uses. These just aren't supported and are unlikely to be supported in the future.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Aug 3, 2023
@crazyair
Copy link
Author

crazyair commented Aug 3, 2023

The initial requirement was

interface TableProps<T = any> {
  // dataIndex support `a | b | [a] | [b]`
  columns: { dataIndex: keyof T | [keyof T]; render: (value: T[keyof T]) => any }[];
}

export const data: TableProps<{ a: string; b: number }> = {
  columns: [
    { dataIndex: ['a'], render: value => value }, // render value type is string
    { dataIndex: ['b'], render: value => value }, // render value type is number
  ],
};

I don't know how to write the type either

@crazyair
Copy link
Author

crazyair commented Aug 3, 2023

Can I convert ['a' , 'b'] to a-b

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants