Type A is not assignable to type A & B & C. Not sure why ts infers with an `&` and not `|`
Unsure why this is happening. I am trying to create a function that can have multiple types and when a different function with a generic passing that argument it should infer it. Example below its a bit easier to understand in code that description.
https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDeAoK6oEsAmAuKAzsAE6YB2A5mhgIb5GmUoC+KKokUAQgstejnolyVDFABGQxlVbtw0AMK9UYwYWFMxAYykiWbDtACqAJQAyAZV4AiAPQxrUAD5QA5La6vnb2wtdzOU0sAfRMAURsAPXtHF1doz294338A43NQsKtEOBceFwU2A3koIIBZGAAFZX4oAG0AaSwyUvMLTIBdfFqmiAAPYAgybAI3aJgvAH5YKFweqH7B4dHkxOmeObFegaGRsZSoaaVNjDIIADcIYn002EqASTCTEwB5EwUXgBEsmvRG5taIXCHUm+BUGDqAGsICB8GQAK4AW3EVy6fDEAgAjAAOMgNGG6TQY840AA28IgBFBUAAFGAaCBSQB7Gh4QEVSqNDoASgQAD5kPVobD1NI0QwRFBmFBbLZCAALJnw0nYKAUCDACQawbEKAcSXieGas4QPagCBgUbAJlatVDK40QaqgDumGA8qg8OIpNGADNiEzEYRnTQKOrrmJWOhWLIUFomWQiFB49hKfgqo9nm8Pt9suiMMkJmDamJMQAGMvFjHVrA4vEEtwTAA0JZrJPJadp9MZLOwvPgAppSCgdCg3eZrIAdDRmNyWzWpfOMMwm1BaqtXFWa+XK-mF1jcfiRa5PEv91B2xSCPg6QyJ33+bTh5Ix3fe5PxLOzxio8vv8k-C3asdyA-dMDrI98BPVxvwXS9O1vHtWX7Qdhx0V8kOwSctClOdW2jWpZFkeNE01AhAwgAAxeEWkQAAeAAVBYdmWQFMgsPlb3dQkKBQvcoFJDUoGCcde3wJiXBo1NfXIU1ahIpN+ggHDEEQ+9xL48FqxEt9WV4UTWUIthtATJMU0pI9eBecQACtlOASdhQIGlzIIblJxk4YaRpSE+MhKAADIAqgEwIAoMI+jAHz3MGIguPlbleRoUZhSZX09XkNLkyZVNRkk4YIE800ZTlGiCC9aBnXlEAsE1LQaDITVyF9K49XlTACHk0zNWCczeFcyyguy3K6gGmEOmMjBMF9GkdMwwLgt6nKIF5LSMQUnr4N4JbUzqHcOkneDRiGnaID2isDqO2aDIfIzmCAA
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
1 Reply
To be honest, i don't know exactly what you're trying to do, but i made what i think you want to do. See:
Since yours payloads appears to always be an object with different keys, I've made an generic
ApiResponse<T>
that pass T as an obligatory key.
Since i also imagine that your api errors will have a default format, I've created APIERRORCODES3<T extends string>
that receives it's format but needs a check to be sure that it's correct(i'm guessing it will be received externally). Also, you change it to be the preferred object or undefined instead of what i did. I just did it this way so i can declare it as a variable {}
to test the function typing. When getting it from a request, you can change for what bests suits you.
Make some runtime type checking, and it returns the payload with the key passed as a generic.
If i'm missing something or you need more help, just let me know.