You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Type 'string' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
There is no type error on return '42'.
π Expected behavior
There should be no type error on the const _. T is indeed of type string in that branch, and can not be never, as it has a .tag property.
There should be a type error on return '42'. T is of type number in that branch, which is incompatible with the type "42".
Additional information about the issue
I know it's a complicated case, but it would let me express pseudo-GADTs powerful enough to do some type of Zod/Runtype-like dynamic types that I can dynamically pattern-match over. That way, I could do well-typed svelte component generation for forms or displays, among other things.
Cheers!
The text was updated successfully, but these errors were encountered:
You get the same error if you put 'lol' as string, the problem is that it is not recognising T as string.
Adding as string does not help, because T can be a more specific type than string. T could be the literal type "foo", in which case assigning any string is not valid.
To give an example, this is your issue: T is typed "bar", but a random string "any string" is tried to be passed:
functionfoo<T>(x: T){}foo<"bar">("any string")
Do you think that narrowing x will cause the type T to narrow too? If so, that's not the case. Narrowing x will only narrow x. Narrowing something typed DynType<T> will not narrow T. That's not how this works, the types exist independently - DynType<T> has no effect on the type T.
π Search Terms
narrowing, discriminated types, extends, generic, constraints
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=4.0.5#code/KYDwDg9gTgLgBDAnmYcAiiB2AVZwA82AfHALwBQAPnABTZygzCYAmAznGzFAJaYDmcAPxwA3uTgIAhoIBccAORdeAhXADcEuAH1tMGXHnK+ggGRx6mgL6G4mYADdgUAJRVa9Rs3Z2ArgFsAI2dhMS19OUVMAOCoNU1JXQjbaKCQ80tyG3l7J1dycgAzX0wAYxgeCEw4FgD-RGxCIhoQWwwcPCaXMMkeQtoQADpk0lHFY1Vu8UlJUqquHVt6UkUAGwhV+K1JKGAYXyhqhXXNjS0rLT6B4YNRlYVU2IVu3f3DxQAWACYtnb2D6qtaxAA
π» Code
π Actual behavior
There is a type error on
const _
:There is no type error on
return '42'
.π Expected behavior
There should be no type error on the
const _
.T
is indeed of typestring
in that branch, and can not benever
, as it has a.tag
property.There should be a type error on
return '42'
.T
is of typenumber
in that branch, which is incompatible with the type"42"
.Additional information about the issue
I know it's a complicated case, but it would let me express pseudo-GADTs powerful enough to do some type of Zod/Runtype-like dynamic types that I can dynamically pattern-match over. That way, I could do well-typed svelte component generation for forms or displays, among other things.
Cheers!
The text was updated successfully, but these errors were encountered: