TypeScript Version: nightly
Code
interfaceTest{optional?: string;}functiongetProperty<KextendskeyofTest>(t: Test,key: K): Test[K]|never{if(t[key]){returnt[key];}thrownewError('Value does not exist at key');}consta: string=getProperty({},'optional');Target behavior:
The given function is coded such that it will always return a non-empty value, so it would be great if there were a way to make something like this compile. Is there a way to communicate the opposite of Partial<T>, e.g. that we knowTest[K] will be non-empty?
Actual behavior:
error TS2322: Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.
TypeScript Version: nightly
Code
Target behavior:
The given function is coded such that it will always return a non-empty value, so it would be great if there were a way to make something like this compile. Is there a way to communicate the opposite of
Partial<T>, e.g. that we knowTest[K]will be non-empty?Actual behavior:
error TS2322: Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.