This repository was archived by the owner on May 31, 2026. It is now read-only.
refactor: enable strict TypeScript and add CI#2
Merged
Conversation
Copilot
AI
changed the title
[WIP] Fix TypeScript type errors and improve type declarations
Enable strict TypeScript mode and resolve core typing/runtime-shape mismatches
May 29, 2026
Member
|
@copilot 补充 GitHub Actions 配置和测试用例。 |
lc-soft
suggested changes
May 29, 2026
Copilot
AI
changed the title
Enable strict TypeScript mode and resolve core typing/runtime-shape mismatches
Enable strict TypeScript mode, add CI/test coverage, and refine widget component typing
May 29, 2026
lc-soft
suggested changes
May 29, 2026
Member
|
@copilot 按照 Angular 规范调整 PR 标题,长度不超过 72 字符。 |
- Drop package-lock.json; switch to Yarn 4 (Berry) via Corepack - Add packageManager field and .yarnrc.yml (nodeLinker: node-modules) - Update .gitignore for Yarn Berry layout - Replace npm run with yarn in package.json scripts - Update CI workflow to use corepack enable + yarn install --immutable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR enables
strictTypeScript checking and resolves the resulting type failures across binding, compile, hook, widget, and JSX runtime layers. The changes focus on removing unsafe inference (never, implicitany, undefined paths) while preserving current runtime behavior.tsconfigstrictnesscompilerOptions.strictand addressed all surfaced errors without widening toanyas a fallback pattern.rootDirfor stable declaration/build layout under current TypeScript behavior.Binding/runtime core (
src/binding.ts)popFunctionComponentto actually pop context state.typeNameMapasPartial<Record<CType, string>>to allow safe enum indexing.compilerself-reference insidecompileFunctionin favor of local function calls.createBinding/createObjectBindinggenerics so proxy-backed metadata + attached data preserve shape under strict mode.applytrap return explicitundefinedto match the intendedValue | voidbehavior path.React tree compilation (
src/compile.ts)attrMapasRecord<string, string>.transformNodeChildrento avoidnever[].transformReactNodeexplicitly returnNode | undefinedand filtered undefined before assigning toNode[].propsaccess (Record<string, any) where dynamic key access is required.compileelement selection to an IIFE soelis always initialized under strict analysis.Nodeshape explicitly withchildren: Node[].State/ref hooks
src/useState.tsisNumericTypewithvalueType !== undefined.CTypeto eliminate unassigned control-flow branches.src/useRef.tsas unknown asdouble assertion by returning a typed intersection (ObjectBinding & WidgetRefBinding).WidgetInstance.valuesetter input type tostring | ObjectBinding.setTextInputValue()and added explicit return typing forgetTextInputValue().Widget components (
src/widgets.tsx)FunctionWidgetfor clearer semantics.const+ arrow function style with typed declarations.shouldPreRenderbehavior.JSX runtime typing (
src/jsx-runtime.ts)key?: React.Key).react/jsx-runtimeexports to avoid declaration conflicts with@types/react.Build/test and CI
build,typecheck, andtest:unit; updatedtestto run unit tests.shouldPreRender), andRouterLinkprop mapping.Original prompt
修复 TypeScript 类型错误、补充缺失类型声明、纠正属性类型被推断为 never 的问题
请按照以下实施计划,对项目中的所有 TypeScript 类型错误进行修复。
一、
tsconfig.json— 启用严格检查在
compilerOptions中添加"strict": true,然后修复因此暴露出来的所有错误。二、
src/binding.ts2.1
applytrap 返回类型不符createBinding内Proxy的applytrap 没有return语句(返回undefined),与BindingBase接口声明的(...arg: Value[]): Value返回类型不符。修复:将
applytrap 的返回类型改为Value | void,或在函数调用结束后return undefined。2.2
popFunctionComponent逻辑 bugpopFunctionComponent内部错误地调用了contextList.push(ctx)而不是contextList.pop()。修复:改为
contextList.pop(),并移除多余的ctx参数(因为pop不需要参数)。2.3
typeNameMap索引类型错误typeNameMap的 key 是字面量联合类型,但用CType(包含所有枚举值)去索引会导致类型错误。修复:
2.4
compileFunction中自引用compiler对象compileFunction内部调用compiler.compileVariableDeclaration和compiler.compileObjectDestroyer,而compiler在文件末尾定义,存在循环引用和类型推断问题。修复:将这两处改为直接调用本地函数
compileVariableDeclaration(item)和compileObjectDestroyer(item.initializer)。2.5
createBinding参数data类型data: Record<string, any> = {}导致Proxytarget 的属性类型推断丢失。修复:改为更具体的类型,或为泛型添加约束。
三、
src/compile.ts3.1
attrMap索引导致key被推断为never修复:
3.2
children数组类型被推断为never[]transformNodeChildren中const children = []因多种类型 push 而被推断为never[]。修复:
3.3
transformReactNode返回Node | undefined,但赋值给Node[]函数多处隐式返回
undefined,赋值给node.children时报类型错误。修复:声明返回类型为
Node | undefined,在赋值时过滤 undefined:同时将
Node类型的children字段声明为Node[](通过修改createNode函数的返回类型)。3.4
el.props类型过于宽泛导致属性访问报错严格模式下
ReactElement的props默认类型为{},动态访问属性会报错。修复:在需要动态访问 props 的地方断言为
Record<string, any>:3.5
compile函数中let el: ReactElement可能未初始化严格模式下 TS 检测到
el在switch语句后可能未赋值。修复:使用立即执行函数(IIFE)或初始赋值确保
el总是有值。四、
src/useState.ts4.1
switch-case穿透导致value可能未赋值number分支在isNumericType(valueType)为 false 时 fall-through 到default,但 TS 无法保证value在所有路径都被赋值。修复:为
number分支的isNumericType判断失败情况明确添加throw或break。4.2
isNumericType(valueType)参数可能为undefinedvalueType?: CType是可选参数,类型为CType | undefined,但isNumericType接受CType,不接受undefined。修复:
五、
src/useRef.ts5.1 移除
as unknown as双重类型断言useRef返回值通过as unknown as { name: string; current: WidgetInstance }强转,绕过类型检查。修复:定义明确的返回类型接口,修正
createObjectBinding调用的数据结构,使返回类型自然匹配,避免双重断言。5.2
WidgetInstance.valuesetter 参数类型setter 参数为
any,getter 返回ObjectBinding,在严格模式下需要显式标注。修复:将 setter 参数类型改为
string | ObjectBinding或其他更具体的类型。六、
src/widgets.tsx6.1 函数组件上挂载
shouldPreRender属性类型缺失修复:定义包含
shouldPreRender属性的函数类型,或使用Object.assign:对所有组件(Widget、Button、Link、Text、TextInput、ScrollArea、ScrollAreaContent、Scrollbar、RouterLink、RouterView)统一处理。
七、
src/jsx-runtime.ts7.1 自定义
JSXFactor类型与@types/react中jsx-runtime官方类型对齐检查
@types/react中react/jsx-runtime的官方jsx/jsxs类型签名,确保自定义JSXFactor与之兼容,避免模块声明覆盖引发的类型冲突。执行顺序
binding.ts(基础类型,其他文件依赖)compile.ts(最多类型错误)useState.tsuseRef.tswidgets.tsxjsx-runtime.tstsconfig.json开启strict: true并验证无剩余错误请确保修改后项目可以通过
tsc --noEmit编译无错误。The following is the prior conversation context from the user's chat exploration (may be truncated):
User: 我想解决这个项目内的 ts 类型错误,补充缺失的类型声明,纠正部分属性类型被推断成 never 的问题。你帮我写一份实施计划吧?
Assistant: 以下是针对
lcui-dev/react项目的 TypeScript 类型错误修复实施计划,按问题严重程度和模块组织:实施计划:修复 TypeScript 类型错误
一、
tsconfig.json— 启用严格检查问题:当前未开启
strict,导致大量隐式类型问题被掩盖。操作:在
compilerOptions中添加"strict": true,然后逐步修复暴露出来的错误(可分步骤启用noImplicitAny、strictNullChecks等)。二、
src/binding.ts2.1
BindingBase函数签名返回类...This pull request was created from Copilot chat.