Skip to content

Typescript fail to parse JSX syntax correctly #61720

Closed
@GuichiZhao

Description

@GuichiZhao
import { FC } from 'react';
type Test<X extends string> = X;
const a: Test<'a'> = {} as Test<'a' | 'b'>;
console.log(a);

type TestFn<X extends string> = (args: X) => void;
const b: TestFn<'a'> = {} as TestFn<'a' | 'b'>;
console.log(b);

type TestFC<X extends string> = FC<{ data: X }>;
const c: TestFC<'a'> = {} as TestFC<'a' | 'b'>;
console.log(c);

const a introduce a type error, which is a desired behaviour because 'a'|'b' is not a subtype of 'a', however, if the type parameter is present in place of the function parameter, there is no error because the parameter is contravariant instead of covariant, TS is smart enough to identify the pattern even in place of generic

However, TS is not smart enough to handle const c: TestFC<'a'> = {} as TestFC<'a' | 'b'>; correctly, it reports a undesired error Type '"a" | "b"' is not assignable to type '"a"'. the root case should be TS fail to notice {data:X} present in function paramter

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions