GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Function fields in class with conditional optionality of parameters · Issue #57139 · microsoft/TypeScript Closed Labels DuplicateAn existing issue was already createdAn existing issue was already created Description🔎 Search Terms"class", "conditinal types", "conditional functions parameters", "optional function parameters" 🕗 Version & Regression Information
⏯ Playground Link💻 Codeclass TestClass<TInput> { // Type '(args?: TInput) => void' is not assignable to // type 'TInput extends undefined ? (args?: TInput | undefined) => void : (args: TInput) => void' foo: TInput extends undefined ? (args?: TInput) => void : (args: TInput) => void = (args?: TInput): void => {} } const optional = new TestClass<undefined>(); optional.foo(); // OK, as expected const required = new TestClass<number>(); required.foo(); // Error, argument for args was not provided, as expected required.foo(1); // OK, as expected // It appears to work outside of class context type Opt<T> = (args?: T) => void; type Req<T> = (args: T) => void; const a: Opt<number> = (args?: number) => {}; const b: Req<number> = (args?: number) => {}; type Check<T> = T extends undefined ? Opt<T> : Req<T>; const c: Check<undefined> = (args?: number) => {}; const d: Check<number> = (args?: number) => {}; // You can wrap as any on the initial value class TestClass2<TInput> { // Type '(args?: TInput) => void' is not assignable to type 'Check<TInput>'. bar: Check<TInput> = (args?: TInput) => {} baz: Check<TInput> = ((args?: TInput) => {}) as any } const optional2 = new TestClass2<undefined>(); optional2.bar(); // OK, as expected optional2.baz(); // OK, as expected const required2 = new TestClass2<number>(); required2.bar(); // Error, argument for args was not provided, as expected required2.baz(); // Error, argument for args was not provided, as expected required2.bar(1); // OK, as expected required2.baz(1); // OK, as expected 🙁 Actual behaviorclass TestClass<TInput> { // Type '(args?: TInput) => void' is not assignable to // type 'TInput extends undefined ? (args?: TInput | undefined) => void : (args: TInput) => void' foo: TInput extends undefined ? (args?: TInput) => void : (args: TInput) => void = (args?: TInput): void => {} } 🙂 Expected behaviorclass TestClass<TInput> { // This should be possible foo: TInput extends undefined ? (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 Additional information about the issueMight be related to #12400 Activity
MetadataMetadataAssigneesLabelsDuplicateAn existing issue was already createdAn existing issue was already created TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |