microsoft / microsoft/TypeScript
This JSX tag's 'children' prop expects type 'string' which requires multiple children, but only a single child was provided
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
🔎 Search Terms
This JSX tag's 'children' prop expects type 'string' which requires multiple children, but only a single child was provided. ts(2745)
ts2745
🕗 Version & Regression Information
- Reproducible in 5.0.3 - 5.8.3
- The error message was different in 4.9.5 and below: "Property 'children' is missing in type '{}' but required in type '{ children: string; }'"
⏯ Playground Link
💻 Code
const React = {createElement, Fragment}
// This JSX tag's 'children' prop expects type 'string' which requires multiple children, but only a single child was provided.ts(2745)
console.log(<StringChildren>a string</StringChildren>);
function StringChildren({ children }: { children: string }) {
return children;
}
// Works fine without children
console.log(<NoChildren />);
function NoChildren() {
// Fragment works fine
return <>a string</>;
}
// I don't know if this is related:
// JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.ts(7026)
console.log(<div />);
// JSX typing is based fully on React 19 typings, but simplified to minimal reproducible example
// Exactly the same error occurs with React typings
// (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts)
export declare function createElement(
sel: any,
data?: any,
text?: any
): JSX.Element;
export declare const Fragment: (props: {
children?: JSX.Element;
}) => JSX.Element;
export namespace JSX {
export type Element =
| string
| number
| bigint
| Iterable<Element>
| boolean
| null
| undefined;
export interface ElementAttributesProperty {}
export interface ElementChildrenAttribute { children?: Element;}
export interface IntrinsicAttributes {}
export interface IntrinsicElements { div: { id?: string };}
}
🙁 Actual behavior
Calling JSX component that accepts string children type emits the following error:
This JSX tag's 'children' prop expects type 'string' which requires multiple children, but only a single child was provided.ts(2745)
🙂 Expected behavior
No error should be emitted
Additional information about the issue
The exact same error is reproducible with official React 19 typings. See tiny reproduction repository: https://github.com/maxpatiiuk/typescript-react-19-jsx-bug
Looking at https://github.com/microsoft/TypeScript/blob/main/src/compiler/checker.ts, the error is triggered by the following check:
// Line 21585:
else if (!isTypeRelatedTo(getIndexedAccessType(source, childrenNameType), childrenTargetType, relation)) {
// arity mismatch
result = true;
const diag = error(
containingElement.openingElement.tagName,
Diagnostics.This_JSX_tag_s_0_prop_expects_type_1_which_requires_multiple_children_but_only_a_single_child_was_provided,
childrenPropName,
typeToString(childrenTargetType),
);
if (errorOutputContainer && errorOutputContainer.skipLogging) {
(errorOutputContainer.errors || (errorOutputContainer.errors = [])).push(diag);
}
}
It appears that getIndexedAccessType(source, childrenNameType) tries to get the indexed type of string, which returns unknown type, which is not considered related to string type.
Potentially related to https://github.com/microsoft/TypeScript/issues/60572, however the suggested workaround in that issue is not working.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Playground またはリンク先のリポジトリで最小再現から始め、次に src/compiler/checker.ts の 21585 行付近にある getIndexedAccessType チェック周辺の JSX children チェック経路を読みます。提供された StringChildren の例と関連する React 19 の型定義で動作を検証します。完了の条件は、有効な単一の文字列 child で ts(2745) が出力されなくなることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 45/100