mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
Merge pull request #6232 from Microsoft/contextuallyTypeJsxStringLiteralAttributes
Contextually type JSX string literal attributes
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx(13,15): error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
|
||||
Type '"f"' is not assignable to type '"C"'.
|
||||
tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx(14,15): error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
|
||||
Type '"f"' is not assignable to type '"C"'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx (2 errors) ====
|
||||
|
||||
namespace JSX {
|
||||
interface IntrinsicElements {
|
||||
span: {};
|
||||
}
|
||||
}
|
||||
|
||||
const FooComponent = (props: { foo: "A" | "B" | "C" }) => <span>{props.foo}</span>;
|
||||
|
||||
<FooComponent foo={"A"} />;
|
||||
<FooComponent foo="A" />;
|
||||
|
||||
<FooComponent foo={"f"} />;
|
||||
~~~~~~~~~
|
||||
!!! error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
|
||||
!!! error TS2322: Type '"f"' is not assignable to type '"C"'.
|
||||
<FooComponent foo="f" />;
|
||||
~~~~~~~
|
||||
!!! error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
|
||||
!!! error TS2322: Type '"f"' is not assignable to type '"C"'.
|
||||
@@ -0,0 +1,30 @@
|
||||
//// [contextuallyTypedStringLiteralsInJsxAttributes01.tsx]
|
||||
|
||||
namespace JSX {
|
||||
interface IntrinsicElements {
|
||||
span: {};
|
||||
}
|
||||
}
|
||||
|
||||
const FooComponent = (props: { foo: "A" | "B" | "C" }) => <span>{props.foo}</span>;
|
||||
|
||||
<FooComponent foo={"A"} />;
|
||||
<FooComponent foo="A" />;
|
||||
|
||||
<FooComponent foo={"f"} />;
|
||||
<FooComponent foo="f" />;
|
||||
|
||||
//// [contextuallyTypedStringLiteralsInJsxAttributes01.jsx]
|
||||
var FooComponent = function (props) { return <span>{props.foo}</span>; };
|
||||
<FooComponent foo={"A"}/>;
|
||||
<FooComponent foo="A"/>;
|
||||
<FooComponent foo={"f"}/>;
|
||||
<FooComponent foo="f"/>;
|
||||
|
||||
|
||||
//// [contextuallyTypedStringLiteralsInJsxAttributes01.d.ts]
|
||||
declare namespace JSX {
|
||||
}
|
||||
declare const FooComponent: (props: {
|
||||
foo: "A" | "B" | "C";
|
||||
}) => any;
|
||||
@@ -0,0 +1,16 @@
|
||||
// @jsx: preserve
|
||||
// @declaration: true
|
||||
|
||||
namespace JSX {
|
||||
interface IntrinsicElements {
|
||||
span: {};
|
||||
}
|
||||
}
|
||||
|
||||
const FooComponent = (props: { foo: "A" | "B" | "C" }) => <span>{props.foo}</span>;
|
||||
|
||||
<FooComponent foo={"A"} />;
|
||||
<FooComponent foo="A" />;
|
||||
|
||||
<FooComponent foo={"f"} />;
|
||||
<FooComponent foo="f" />;
|
||||
Reference in New Issue
Block a user