Comments (9)
There's definitely many duplicates of this
#27808, extends oneof
issue, I've seen too many of them.
@noahtallen just use the distributive behavior to make it work
function doSomethingWithData<T extends SomeThings>({thing, data}: T extends unknown ? ArgType<T> : never) {
if (thing === SomeThings.One) {
console.log(data.x); // OK
}
}
Note that you also have to make the literal object foo
declared as const.
const foo = {thing: SomeThings.One, data: {x: 1}} as const;
from typescript.
Hm interesting. It's odd to me that something like T extends T ? ... : never
is required to get inference to work, given that T always extends T. So this is basically just tricking/forcing the compiler to distribute here?
Either way, happy that there's a solution!
from typescript.
Because within the function the compiler doesn't know which type is passed along. It could be SomeThings.One
, it could be SomeThings.Two
, it could also be the type SomeThings.One | SomeThings.Two
. There's definitely many duplicates of this, just don't have one at hand right now.
from typescript.
The compiler should be able to know that SomeThings.One
was passed within this block. It should also be know (based on the contracted specified by the ArgType
interface) that it's impossible to pass mismatched arguments where the data doesn't match the enum:
// We know which one was passed now:
if (thing === SomeThings.One) {
console.log(data.x);
}
// Correctly shows an error, so it does narrow the type of `data` based on the enum here:
doSomethingWithData({thing: SomeThings.One, data: {y: 'xyz'});
I think it evaluates ArgType<T>
too early into a union of all possible values, where if it evaluated it within the if statement, it should know that it resolves to a single possibility
from typescript.
You misunderstand how generics work. Just because thing
equals SomeThings.One
does not mean you're dealing with a { x: number }
data.
This is a perfectly valid and legal call:
doSomethingWithData<SomeThings.One | SomeThings.Two>({ thing: SomeThings.One, data: { y: "abc" }})
from typescript.
That's great, thanks for sharing! looks like as const
isn't needed if you do:
const foo: ArgType<SomeThings.One> = {thing: SomeThings.One, data: {x: 1}};
if (someCondition) {
foo.data.x = 100;
}
// works!
doSomethingWithData(foo);
doSomethingWithData({ thing: SomeThings.Two, data: {y: 'hi'}});
Anyways, I'm curious how this works. Why T extends unknown
when T is known?
from typescript.
Why T extends unknown when T is known?
Because unknown
is the top type that anything can be assigned to it, to make sure that the conditional type always get in the "true" branch. You can replace it with T extends any
or T extends T
, all of them are OK.
And for "distributive" behavior, see https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types.
from typescript.
So this is basically just tricking/forcing the compiler to distribute here?
Yes. Conditional types distribute over type parameters, so:
type ArrayOf<T> = T extends T ? T[] : never;
type Test = ArrayOf<string | number>; // string[] | number[];
whereas without distribution you’d get (string | number)[]
instead.
from typescript.
from typescript.
Related Issues (20)
- Incorrect Error Message: "Cannot assign to 'name' because it is a constant" Instead of "Identifier 'name' is not defined" for Undefined Identifier Assignment HOT 2
- Generic with extends yields to different type than directly specifying it HOT 5
- Instantiation expressions don't reject incompatible signatures in situations with mixed call and construct signatures HOT 1
- Parameters of overridden methods are made implicit any when using JSDoc `@override` HOT 13
- Auto complete and auto import not work (ReactJS, ChakraUI)
- 'TypeScript and JavaScript Language Features' Formatter hangs HOT 3
- Mapped type of a conditional type unexpectedly fails inference
- Returing a generic argument from a function allows returning a wrong type HOT 1
- Module resolution behaves differently when typeRoots is specified
- Ability to type check setters in interface implementations HOT 2
- ⚡ Performance: Project service spends excess time cleaning client files when called synchronously HOT 2
- ⚡ Performance: Project service doesn't cache all fs.statSync HOT 18
- jsxRuntime pragma does not work
- ThisType doesn't enforce type-checking `Object.prototype.call` HOT 2
- ⚡ Performance: Project service doesn't cache all fs.realpath HOT 1
- `null` prop with `satisfies`, `strictNullChecks: false` + `noImplicitAny: true` gives any report HOT 4
- Debugging failure caused by lack of backtick(`)
- [proposal] Non widened string values should be valid enum values, like widened string values HOT 1
- 'Could not find source file' with Vue plugin when creating new files and using VS Code's file watcher HOT 7
- Contextual parameters inferred from overloads improvements
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.