-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Narrowing type of an object property with type guard does not allow us to assign/return the instance as having a narrower prop type. #32595
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
Duplicate of #31755. |
From #31755 This reasoning seems flawed to me. I'm using TS because I need safe code. If expensive compilation was a problem - I'd be using plain JS. |
@IKoshelev There is a balancing act here.. how long are you willing to wait for compilation ? 5s, 10s ? 60s ? How long are you willing to wait for a code completion window to appear before it becomes useless ? 0.5s, 1s? 10s ? Not adding features that have limited impact but can seriously degrade performance, keeps typescript usable for all of us. |
@dragomirtitian I don't see how this involves intellisense - I'm returning a var and only its name really needs completion. Once I'm done typing return var name - that's when I expect compiler to check whenever its type is suitable. If it warns me about error in type of 'return' 10 seconds later - it is not a problem compared to the alternative of explicitly casting my instance. I mean, with all the time investment into building unit tests and running them before commits, getting a type error 10 seconds later does not seem like a problem to me. With the --incremental flag this also won't slow down the build AFAIU - whatever check is made, will only be made once per change of this file. P.S. Maybe you can suggest a type-safe alternative to explicit cast in the code above? |
Never mind, got it export type NarrowPropTypeByKey<TTarget,
TKey extends keyof TTarget,
TNarrow extends TTarget[TKey]> = {
[key in keyof TTarget]:
key extends TKey
/**/ ? TNarrow extends TTarget[key]
/* */ ? TNarrow
/* */ : TTarget[key]
/**/ : TTarget[key]
};
export function narrowPropType<
T,
TKey extends keyof T,
TNarrowedPropType extends T[TKey]>
(inst: T, key: TKey, guard: (wide: T[TKey]) => wide is TNarrowedPropType): inst is NarrowPropTypeByKey<T, TKey, TNarrowedPropType> {
const val = inst[key];
if(guard(val)) {
return true;
}
return false;
} |
TypeScript Version: 3.5.1, 3.5.3
Search Terms: guard type-guard property return assignment
Code
Expected behavior:
Successful type guard check narrowing property
prop
fromstring | number
tostring
should allow us to return \ assign containing object instance when a{ prop: string }
type is expected.Actual behavior:
With successful type guard check we are allowed to assign property
prop
whenstring
is expected, but assigning \ returning containing instance as{ prop: string }
is still forbidden.Playground Link: https://www.typescriptlang.org/play/#code/C4TwDgpgBA6glgEwgBQE4HsxQLxQN4BQUUYGYAXFABQB2ArgLYBGEqUAPlAM7Cpw0BzAJQBuAgF8CBUJCgA5AIaoMAdzSYc+IiTKUefQWMkEAZnRoBjYHHQ1uAC3QqLCrhCpDKi5U-VZCxBa2PFAqiBCU8Eh+mgHEOpiUAOQKqUlQrtT6-AIcUPTMrELa4mLaQTQhAnRKCJpUcJS0jCxsnNmCQp5QcD1c3Lw5OAB8ULx0EGXEcCZU1bVUYUgAdKSYXXHEAPRbY-Zw-SroqADWXNqBwcAJFAMGubhLEKtkU-E7ewdQCOgQ-TToYAAfguUAANhBrjQlKpKHgbnpBoIoOJNE83ttdgoaNdgPt+j8-vlASD4lBUJC6Kg7OiSlAJEA
Related Issues: #3812
The text was updated successfully, but these errors were encountered: