mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-09 16:39:46 -05:00
Fix 10625: JSX Not validating when index signature is present (#10352)
* Check for type of property declaration before using index signature * Add tests and baselines * fix linting error
This commit is contained in:
@@ -10140,10 +10140,9 @@ namespace ts {
|
||||
const correspondingPropSymbol = getPropertyOfType(elementAttributesType, node.name.text);
|
||||
correspondingPropType = correspondingPropSymbol && getTypeOfSymbol(correspondingPropSymbol);
|
||||
if (isUnhyphenatedJsxName(node.name.text)) {
|
||||
// Maybe there's a string indexer?
|
||||
const indexerType = getIndexTypeOfType(elementAttributesType, IndexKind.String);
|
||||
if (indexerType) {
|
||||
correspondingPropType = indexerType;
|
||||
const attributeType = getTypeOfPropertyOfType(elementAttributesType, getTextOfPropertyName(node.name)) || getIndexTypeOfType(elementAttributesType, IndexKind.String);
|
||||
if (attributeType) {
|
||||
correspondingPropType = attributeType;
|
||||
}
|
||||
else {
|
||||
// If there's no corresponding property with this name, error
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
tests/cases/conformance/jsx/file.tsx(14,28): error TS2322: Type 'number' is not assignable to type 'string'.
|
||||
tests/cases/conformance/jsx/file.tsx(16,28): error TS2322: Type 'boolean' is not assignable to type 'string | number'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/jsx/react.d.ts (0 errors) ====
|
||||
|
||||
declare module JSX {
|
||||
interface Element { }
|
||||
interface IntrinsicElements {
|
||||
div: any;
|
||||
}
|
||||
interface ElementAttributesProperty { prop: any }
|
||||
}
|
||||
|
||||
==== tests/cases/conformance/jsx/file.tsx (2 errors) ====
|
||||
|
||||
interface IProps {
|
||||
primaryText: string,
|
||||
[propName: string]: string | number
|
||||
}
|
||||
|
||||
function VerticalNavMenuItem(prop: IProps) {
|
||||
return <div>props.primaryText</div>
|
||||
}
|
||||
|
||||
function VerticalNav() {
|
||||
return (
|
||||
<div>
|
||||
<VerticalNavMenuItem primaryText={2} /> // error
|
||||
~~~~~~~~~~~~~~~
|
||||
!!! error TS2322: Type 'number' is not assignable to type 'string'.
|
||||
<VerticalNavMenuItem justRandomProp={2} primaryText={"hello"} /> // ok
|
||||
<VerticalNavMenuItem justRandomProp1={true} primaryText={"hello"} /> // error
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2322: Type 'boolean' is not assignable to type 'string | number'.
|
||||
</div>
|
||||
)
|
||||
}
|
||||
47
tests/baselines/reference/tsxAttributeResolution14.js
Normal file
47
tests/baselines/reference/tsxAttributeResolution14.js
Normal file
@@ -0,0 +1,47 @@
|
||||
//// [tests/cases/conformance/jsx/tsxAttributeResolution14.tsx] ////
|
||||
|
||||
//// [react.d.ts]
|
||||
|
||||
declare module JSX {
|
||||
interface Element { }
|
||||
interface IntrinsicElements {
|
||||
div: any;
|
||||
}
|
||||
interface ElementAttributesProperty { prop: any }
|
||||
}
|
||||
|
||||
//// [file.tsx]
|
||||
|
||||
interface IProps {
|
||||
primaryText: string,
|
||||
[propName: string]: string | number
|
||||
}
|
||||
|
||||
function VerticalNavMenuItem(prop: IProps) {
|
||||
return <div>props.primaryText</div>
|
||||
}
|
||||
|
||||
function VerticalNav() {
|
||||
return (
|
||||
<div>
|
||||
<VerticalNavMenuItem primaryText={2} /> // error
|
||||
<VerticalNavMenuItem justRandomProp={2} primaryText={"hello"} /> // ok
|
||||
<VerticalNavMenuItem justRandomProp1={true} primaryText={"hello"} /> // error
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
//// [file.jsx]
|
||||
function VerticalNavMenuItem(prop) {
|
||||
return <div>props.primaryText</div>;
|
||||
}
|
||||
function VerticalNav() {
|
||||
return (<div>
|
||||
<VerticalNavMenuItem primaryText={2}/> // error
|
||||
// error
|
||||
<VerticalNavMenuItem justRandomProp={2} primaryText={"hello"}/> // ok
|
||||
// ok
|
||||
<VerticalNavMenuItem justRandomProp1={true} primaryText={"hello"}/> // error
|
||||
// error
|
||||
</div>);
|
||||
}
|
||||
32
tests/cases/conformance/jsx/tsxAttributeResolution14.tsx
Normal file
32
tests/cases/conformance/jsx/tsxAttributeResolution14.tsx
Normal file
@@ -0,0 +1,32 @@
|
||||
//@jsx: preserve
|
||||
//@module: amd
|
||||
|
||||
//@filename: react.d.ts
|
||||
declare module JSX {
|
||||
interface Element { }
|
||||
interface IntrinsicElements {
|
||||
div: any;
|
||||
}
|
||||
interface ElementAttributesProperty { prop: any }
|
||||
}
|
||||
|
||||
//@filename: file.tsx
|
||||
|
||||
interface IProps {
|
||||
primaryText: string,
|
||||
[propName: string]: string | number
|
||||
}
|
||||
|
||||
function VerticalNavMenuItem(prop: IProps) {
|
||||
return <div>props.primaryText</div>
|
||||
}
|
||||
|
||||
function VerticalNav() {
|
||||
return (
|
||||
<div>
|
||||
<VerticalNavMenuItem primaryText={2} /> // error
|
||||
<VerticalNavMenuItem justRandomProp={2} primaryText={"hello"} /> // ok
|
||||
<VerticalNavMenuItem justRandomProp1={true} primaryText={"hello"} /> // error
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user