-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Mapped Object Type with Conditional Types - Takes Neither #26931
Comments
This is working (mostly) as intended. Anyway, you probably want a non-distributive conditional type: type TrueOrFalse<T> = [T] extends [any] ? true : false; |
Hello @mattmccutchen, at<K extends keyof C>(path: C extends Array<any> ? number : K): DoesntMatterType Basically if C is array take a number to index it, else take a key of C to index it. How can i fix the typings to continue to work for everything else but also to solve this case. EDIT: TL;DR Also, it also makes sense that T[K] should be the index type of T, therefore shouldn't the conditional type choose a branch based on the index type of T[K] ? If someone could explain that to me that would also help my understanding. |
@ShanonJackson Why isn't your at<K extends keyof C>(path: K): DoesntMatterType After all, when There is currently no way your |
Bingo, back when i wrote that i think keyof T was just resolving to string (i think). EDIT: It would be nice for some documentation on how non-distributive conditional types work and why. Can't spread T[K] (spread types may only be created from object types. |
@ShanonJackson You can read about distributive conditional types in the original PR #21316. |
Would you be able to explain these to me..... interface Foo {
name: "foo";
siblings: number[]
}
interface Bar {
name: "bar";
siblings: string[]
}
type Test = {
foo: Foo;
bar: Bar;
}
const getFromTest = <K extends keyof Test>(key: K) => {
const t = ("" as any as Test)[key]
const yolo = {...t} // not spreadable? why.
const test = t.siblings.filter((x) => "" === x.name) // cannot invoke .....
// why shouldn't X become number & string? or number | string
} Ontop of these 2 i have 2-3 more issues that's making the typing of certain functions impossible in my project, also related to the T[K] problem, but it's going to invole bigger repro steps |
@ShanonJackson Please take this to Stack Overflow. |
TypeScript Version: 3.0.3
Search Terms: - Mapped Conditional Types, Mapped Types Bug
Code
Expected behavior:
TrueOrFalse<Test[K]> should be either true or false but comes out as neither. regardless of the type of "any" in the type alias.
Actual behavior:
It chooses neither of the conditional types.
Playground Link:
https://agentcooper.github.io/typescript-play/#code/MYGwhgzhAEBiD29oG8CwAoaXoDswFsBTaAXmgCIAzRcgbgwF8NRIYAhMAJxQ21wOJlyAIy51GGAC4BPAA7EAKoQiTSPTNmrwAXHET0NWUZ10dOBpugD0V6ABN4ynAHJV+MJMmFuAdwAWHtAy8hRgONLk0H7exACWEAA00LGqkmAA1sq4hCnRnAB0UnKKnACuhADynLBgIBCEADwKAHxqCtCEAB5eOHYwYdLQAPxBZcS6lLX1BsDwOCrQAOaEkrCc8PhKC2QNANId3YS9MJnS8JTQW5LNABSnursAlKStaIbQs-OqVwrFAIy6BRjKo1OqNK4AbV2AF1WmRJGNaNAbNBJrE6rxsJ8Fj9igAmQHA6pTcHKSRQ2FqSZgpHI2xojHvbHfMm-eQAZkJ5RBJKaZIpcIokUg0AGopgCPK0AAZKiSXS5eiIJisMzLqzigAWLmVYlgvkqAVqcjC-rhcWjKUAHzlYIVDOV6EsQA
The text was updated successfully, but these errors were encountered: