Merge pull request #6232 from Microsoft/contextuallyTypeJsxStringLiteralAttributes

Contextually type JSX string literal attributes
This commit is contained in:
Daniel Rosenwasser
2016-01-04 14:50:09 -05:00
4 changed files with 87 additions and 14 deletions

View File

@@ -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"'.

View File

@@ -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;

View File

@@ -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" />;