Skip to content

Parameter inference breaks type intersection #38183

Closed
@Ghirigoro

Description

@Ghirigoro

TypeScript Version: 3.8.3 (also tested with 3.9.0-dev.20200425)

Search Terms:
inference

Code

type Effect<T, M> = {
  // NOTE: The problem seems to be here somewhere. Getting rid of the
  // intersection `& M` below gets rid of the error.
  do?: (o: T & M) => void;
};

type Obj = {};

function create<T, M>(params: {
  methods: M;
  effect: Effect<T, M>;
}): Obj & M {
  return params.methods
}

const obj = create({
  // If the parameter below is inferred then the Obj type
  // breaks and the methods are no longer merged in.
  // If a type is given (e.g. `obj:any`) the methods work
  // as expected
  effect:{do: (obj) => {}},
  methods: {
    something() {}
  }
});

// This will be an error if the effect infers the parameter type
obj.something();

Expected behavior:
Functions in methods should be merged into the type that is output by create or an error should be generated if there is a problem with the types.

Actual behavior:
Function in methods are only merged if do in effects is defined with explicitly typed parameters and no error is generated.

Playground Link: playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions