-
Couldn't load subscription status.
- Fork 13.1k
Open
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
π Search Terms
keyof, conditional type, generic
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about distributive conditional types (Generic conditional type T extends never ? 'yes' : 'no' resolves to never when T is never.Β #31751)
β― Playground Link
π» Code
export interface ServiceCredentials {
["google"]: {foo:string};
// more providers here, truncated for brevity
}
export type JSONCompatible<T> =
[T] extends [Date]
//or
// T extends Date
? never
// more logic here, truncated for brevity
: T
export declare function toJson<T>(val: JSONCompatible<T>): void;
function handleSelectedCredential<T extends keyof ServiceCredentials>(credType: T, cred: ServiceCredentials) {
const thisCredential: ServiceCredentials[T] = cred[credType];
toJson<ServiceCredentials[T]>(thisCredential);
}π Actual behavior
Argument of type 'ServiceCredentials[T]' is not assignable to parameter of type 'JSONCompatible<ServiceCredentials[T]>'.
Type '{ foo: string; }' is not assignable to type 'JSONCompatible<ServiceCredentials[T]>'.(2345)
π Expected behavior
I would expect the file to compile.
This similar code that does not use a generic compiles:
declare const x: keyof ServiceCredentials
function handleSelectedCredential(cred: ServiceCredentials) {
const thisCredential: ServiceCredentials[typeof x] = cred[x];
toJson<ServiceCredentials[typeof x]>(thisCredential);
}
Additional information about the issue
Note that I have tried both versions of JSONCompatible, taking into account distributive conditional types:
export type JSONCompatible<T> = [T] extends [Date] ? never : T
export type JSONCompatible<T> = T extends Date ? never : T
The result is the same for me.
The intended behavior of the original version of JSONCompatibleΒ is to prohibit values that don't deserialize to the same value (e.g. JSON.parse(JSON.stringify(new Date())), originally it contained more code. This is the minimal example.
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed