Skip to content

TS2322: inferred string instead value type since TS3.2 #28835

@donaldpipowitch

Description

@donaldpipowitch

TypeScript Version:

Errors: 3.3.0-dev.20181204, 3.2.1
Works: 3.1.6

Search Terms:

3.2, SFC, React, default, params, value type, string

Code

import * as React from 'react';

type AorB = 'a' | 'b';

type Props1 = {
  aOrB: AorB;
}

type Props2 = {
  aOrB?: AorB;
}

export const Comp1: React.SFC<Props1> = () => null;

export const Comp2: React.SFC<Props2> = ({ aOrB = 'a' }) => <Comp1 aOrB={aOrB} />;
{
  "compilerOptions": {
    "jsx": "react",
    "noEmit": true
  }
}
{
  "name": "ts",
  "version": "1.0.0",
  "license": "MIT",
  "scripts": {
    "type-check": "tsc"
  },
  "devDependencies": {
    "@types/react": "^16.7.13",
    "typescript": "3.x.x"
  }
}

Expected behavior:

For 3.1:

$ yarn type-check
yarn run v1.12.3
$ tsc
✨  Done in 3.62s.

Actual behavior:

For >=3.2:

$ yarn type-check
yarn run v1.12.3
$ tsc
src/index.tsx:15:68 - error TS2322: Type 'string' is not assignable to type 'AorB'.

15 export const Comp2: React.SFC<Props2> = ({ aOrB = 'a' }) => <Comp1 aOrB={aOrB} />;
                                                                      ~~~~

  src/index.tsx:6:3
    6   aOrB: AorB;
        ~~~~
    The expected type comes from property 'aOrB' which is declared here on type 'IntrinsicAttributes & Props1 & { children?: ReactNode; }'


Found 1 error.

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Playground Link:

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: check: Contextual TypesThe issue relates to contextual typesFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions