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
classTestClass<TInput>{// Type '(args?: TInput) => void' is not assignable to // type 'TInput extends undefined ? (args?: TInput | undefined) => void : (args: TInput) => void'foo: TInputextendsundefined
? (args?: TInput)=>void
: (args: TInput)=>void=(args?: TInput): void=>{}}constoptional=newTestClass<undefined>();optional.foo();// OK, as expectedconstrequired=newTestClass<number>();required.foo();// Error, argument for args was not provided, as expectedrequired.foo(1);// OK, as expected// It appears to work outside of class contexttypeOpt<T>=(args?: T)=>void;typeReq<T>=(args: T)=>void;consta: Opt<number>=(args?: number)=>{};constb: Req<number>=(args?: number)=>{};typeCheck<T>=Textendsundefined ? Opt<T> : Req<T>;constc: Check<undefined>=(args?: number)=>{};constd: Check<number>=(args?: number)=>{};// You can wrap as any on the initial valueclassTestClass2<TInput>{// Type '(args?: TInput) => void' is not assignable to type 'Check<TInput>'.bar: Check<TInput>=(args?: TInput)=>{}baz: Check<TInput>=((args?: TInput)=>{})asany}constoptional2=newTestClass2<undefined>();optional2.bar();// OK, as expectedoptional2.baz();// OK, as expectedconstrequired2=newTestClass2<number>();required2.bar();// Error, argument for args was not provided, as expectedrequired2.baz();// Error, argument for args was not provided, as expectedrequired2.bar(1);// OK, as expectedrequired2.baz(1);// OK, as expected
🙁 Actual behavior
classTestClass<TInput>{// Type '(args?: TInput) => void' is not assignable to // type 'TInput extends undefined ? (args?: TInput | undefined) => void : (args: TInput) => void'foo: TInputextendsundefined
? (args?: TInput)=>void
: (args: TInput)=>void=(args?: TInput): void=>{}}
🙂 Expected behavior
classTestClass<TInput>{// This should be possiblefoo: TInputextendsundefined
? (args?: TInput)=>void
: (args: TInput)=>void=(args?: TInput): void=>{}}
I would expect this to work because it works outside of class definitions, and because having the implementation function args be optional should be able to handle both cases.
🔎 Search Terms
"class", "conditinal types", "conditional functions parameters", "optional function parameters"
🕗 Version & Regression Information
⏯ Playground Link
https://www.typesc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behavior
🙂 Expected behavior
I would expect this to work because it works outside of class definitions, and because having the implementation function
args
be optional should be able to handle both cases.Additional information about the issue
Might be related to #12400
The text was updated successfully, but these errors were encountered: