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

3.5 regression concerning type inference for non-inferable types #31735

Closed
eps1lon opened this issue Jun 3, 2019 · 2 comments
Closed

3.5 regression concerning type inference for non-inferable types #31735

eps1lon opened this issue Jun 3, 2019 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@eps1lon
Copy link
Contributor

eps1lon commented Jun 3, 2019

TypeScript Version: [email protected]

Search Terms:
3.5, infer

Code

// mocked CssProperties type
type CssProperties = { position: "absolute" | "relative" };

type StylesObject<ClassKey extends string> = Record<ClassKey, CssProperties>;
type PropsBasedstyles<ClassKey extends string, Props extends object> = (
  props: Props
) => StylesObject<ClassKey>;
type Styles<ClassKey extends string, Props extends object> =
  | StylesObject<ClassKey>
  | PropsBasedstyles<ClassKey, Props>;

function createStyles<ClassKey extends string, Props extends object>(
  styles: Styles<ClassKey, Props>
): Styles<ClassKey, Props> {
  return styles;
}

const styles = createStyles({
  root: {
    position: "absolute"
  }
});

Expected behavior:
Behavior in 3.4.5
typeof styles = Styles<'root', {}>

Actual behavior:
Behavior in >=3.5.0
typeof styles = Styles<'root', object>

Playground Link:
playground

Related Issues: Did not find any

Notes:

Seems like the playground is still using 3.4.

Usage of this behavior is showcased in https://github.com/eps1lon/ts-inference-3.5/blob/master/index.ts with a fix for 3.5.

@ahejlsberg
Copy link
Member

This is an effect of #30637, a known breaking change. In your example we make no inferences for the Props type parameter in the call to createStyles. This means we default to unknown, but since that doesn't satisfy the constraint object, we pick object. Previously we'd default to {} which did satisfy the constraint.

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 3, 2019
@eps1lon
Copy link
Contributor Author

eps1lon commented Jun 4, 2019

Suspected as much though I wasn't aware how this interacts with non-inferrable types. Thanks for the explanation.

@eps1lon eps1lon closed this as completed Jun 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants