Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter inference breaks type intersection #38183

Closed
Ghirigoro opened this issue Apr 25, 2020 · 1 comment
Closed

Parameter inference breaks type intersection #38183

Ghirigoro opened this issue Apr 25, 2020 · 1 comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@Ghirigoro
Copy link

Ghirigoro commented Apr 25, 2020

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

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jun 2, 2020
@RyanCavanaugh
Copy link
Member

The problem here is that you have no inference site for T, so T gets fixed to unknown, then this ends up with M getting unknown because of inference on the obj parameter. The straightforward fix is to just remove T from these declarations entirely. A more fleshed-out example might be something we could provide a better way to write a declaration for

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants