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
Note: I'll say "reverse type" a lot in this issue. In such context, the reversed type of a number is string, and the reversed type of a string is a number.
I have the following code:
typeId=string|number;typeObjectId<TextendsId>={id: T;};typeValidObjectId=ObjectId<string>|ObjectId<number>typeReverseId<TextendsValidObjectId>=T['id']extendsnumber ? string : number;functionencodeObjectId<TextendsObjectId<number>>(obj: T): ReverseId<T>{constid=obj.id;// Why does the following does not accept a string type?// As it's the reverse of type number, because of the `ReverseId` type, it should be working.returnid.toString();}
In it, I have the type ReverseId, which converts the type of the id property. For example, if the given object id property has a number type, it will return string; and if the given object's id property has a string type, the number type will be returned.
And that actually works:
typeId=string|number;typeObjectId<TextendsId>={id: T;};typeValidObjectId=ObjectId<string>|ObjectId<number>typeReverseId<TextendsValidObjectId>=T['id']extendsnumber ? string : number;// The `t` constant has a `number` type, which is the "reverse" of the `string`// type, used in the given `ObjectId`.constt: ReverseId<ObjectId<string>>=1;
The problem is: when the ObjectId comes from a generic, as we saw in the encodeObjectId function in the first example, the type does not work.
I think that this is a problem, because it work when the ObjectId type does not come from a generic, and don't work when this type comes from a generic.
The text was updated successfully, but these errors were encountered:
Conditional types ignore the constraints on type parameters for reasons below:
// We check instantiations of the two// types with type parameters mapped to their restrictive form, i.e. a form of the type parameter// that has no constraint. This ensures that, for example, the type// type Foo<T extends { x: any }> = T extends { x: string } ? string : number// doesn't immediately resolve to 'string' instead of being deferred.
and as a result ReverseID<T> does not eagerly reduce to string.
TypeScript Version: 3.7-beta
Search Terms: conditional types generic problem
I have the following code:
Playground link.
In it, I have the type
ReverseId
, which converts the type of theid
property. For example, if the given objectid
property has anumber
type, it will returnstring
; and if the given object'sid
property has astring
type, thenumber
type will be returned.And that actually works:
Playground link.
The problem is: when the
ObjectId
comes from a generic, as we saw in theencodeObjectId
function in the first example, the type does not work.I think that this is a problem, because it work when the
ObjectId
type does not come from a generic, and don't work when this type comes from a generic.The text was updated successfully, but these errors were encountered: