Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💅 React TypeScript issue on The React variable is undeclared #3998

Closed
1 task done
elawad opened this issue Sep 19, 2024 · 13 comments
Closed
1 task done

💅 React TypeScript issue on The React variable is undeclared #3998

elawad opened this issue Sep 19, 2024 · 13 comments

Comments

@elawad
Copy link

elawad commented Sep 19, 2024

Environment information

CLI:
  Version:                      1.9.1
  Color support:                true

Platform:
  CPU Architecture:             aarch64
  OS:                           macos

Environment:
  BIOME_LOG_PATH:               unset
  BIOME_LOG_PREFIX_NAME:        unset
  BIOME_CONFIG_PATH:            unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v20.16.0"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "yarn/1.22.22"

Biome Configuration:
  Status:                       Loaded successfully
  Formatter disabled:           false
  Linter disabled:              false
  Organize imports disabled:    false
  VCS disabled:                 false

Linter:
  JavaScript enabled:           true
  JSON enabled:                 true
  CSS enabled:                  true
  GraphQL enabled:              false
  Recommended:                  false
  All:                          false
  Enabled rules:
  performance/noDelete
  suspicious/noCatchAssign
  suspicious/noUnsafeNegation
  complexity/useLiteralKeys
  suspicious/noClassAssign
  style/useImportType
  complexity/noMultipleSpacesInRegularExpressionLiterals
  a11y/useValidLang
  complexity/noUselessEmptyExport
  suspicious/useNamespaceKeyword
  suspicious/useValidTypeof
  a11y/useValidAriaRole
  correctness/noConstantCondition
  a11y/useAriaActivedescendantWithTabindex
  suspicious/noAssignInExpressions
  style/useDefaultParameterLast
  complexity/noEmptyTypeParameters
  correctness/noConstructorReturn
  style/useSelfClosingElements
  suspicious/noDuplicateParameters
  suspicious/noDuplicateSelectorsKeyframeBlock
  correctness/noUnknownProperty
  style/useTemplate
  correctness/noUnusedLabels
  complexity/noUselessTernary
  correctness/noUnreachableSuper
  suspicious/noCompareNegZero
  suspicious/noExplicitAny
  correctness/noSwitchDeclarations
  a11y/noAutofocus
  correctness/noUnsafeOptionalChaining
  correctness/noConstAssign
  suspicious/noControlCharactersInRegex
  complexity/noUselessTypeConstraint
  style/noVar
  suspicious/noDoubleEquals
  suspicious/noRedundantUseStrict
  style/useLiteralEnumMembers
  suspicious/noGlobalIsNan
  suspicious/noEmptyInterface
  suspicious/noConstEnum
  suspicious/noMisleadingCharacterClass
  correctness/noPrecisionLoss
  a11y/noLabelWithoutControl
  suspicious/noRedeclare
  correctness/noStringCaseMismatch
  correctness/noSetterReturn
  correctness/noInvalidConstructorSuper
  suspicious/noImplicitAnyLet
  suspicious/noFallthroughSwitchClause
  suspicious/noUnsafeDeclarationMerging
  complexity/noUselessThisAlias
  correctness/noUnreachable
  a11y/useKeyWithClickEvents
  suspicious/noDuplicateObjectKeys
  complexity/noThisInStatic
  complexity/useOptionalChain
  correctness/noInnerDeclarations
  style/noParameterAssign
  suspicious/noDuplicateCase
  a11y/useValidAnchor
  complexity/useRegexLiterals
  correctness/noSelfAssign
  correctness/noInvalidBuiltinInstantiation
  style/noUselessElse
  style/useShorthandFunctionType
  suspicious/noShadowRestrictedNames
  correctness/noInvalidDirectionInLinearGradient
  suspicious/noImportantInKeyframe
  a11y/useMediaCaption
  complexity/noUselessLabel
  complexity/noUselessCatch
  correctness/noUnsafeFinally
  a11y/useAriaPropsForRole
  correctness/noNonoctalDecimalEscape
  style/useEnumInitializers
  a11y/useHtmlLang
  suspicious/noDuplicateTestHooks
  complexity/noStaticOnlyClass
  style/useWhile
  complexity/useArrowFunction
  style/noInferrableTypes
  a11y/noNoninteractiveTabindex
  complexity/useSimpleNumberKeys
  correctness/useYield
  a11y/noInteractiveElementToNoninteractiveRole
  style/useNumericLiterals
  correctness/noUnnecessaryContinue
  suspicious/noApproximativeNumericConstant
  suspicious/noImportAssign
  suspicious/noLabelVar
  correctness/noGlobalObjectCalls
  suspicious/useDefaultSwitchClauseLast
  correctness/noUnknownUnit
  a11y/useAltText
  correctness/noEmptyCharacterClassInRegex
  suspicious/noSparseArray
  a11y/useIframeTitle
  complexity/noBannedTypes
  a11y/noSvgWithoutTitle
  correctness/noVoidElementsWithChildren
  style/useAsConstAssertion
  correctness/useJsxKeyInIterable
  style/useExportType
  complexity/noUselessLoneBlockStatements
  suspicious/noDebugger
  style/noArguments
  a11y/useValidAriaValues
  suspicious/noCommentText
  a11y/useFocusableInteractive
  correctness/noUnmatchableAnbSelector
  suspicious/noGlobalAssign
  suspicious/noDuplicateJsxProps
  suspicious/noMisleadingInstantiator
  a11y/noPositiveTabindex
  correctness/noEmptyPattern
  complexity/noExcessiveNestedTestSuites
  security/noDangerouslySetInnerHtmlWithChildren
  a11y/useKeyWithMouseEvents
  suspicious/noExtraNonNullAssertion
  suspicious/noShorthandPropertyOverrides
  correctness/noRenderReturnValue
  correctness/useExhaustiveDependencies
  security/noGlobalEval
  style/noNonNullAssertion
  a11y/noRedundantRoles
  complexity/useFlatMap
  correctness/useIsNan
  correctness/noUnusedVariables
  style/useConst
  suspicious/noGlobalIsFinite
  suspicious/noSelfCompare
  suspicious/noAsyncPromiseExecutor
  suspicious/noDuplicateFontNames
  suspicious/noThenProperty
  suspicious/useGetterReturn
  security/noDangerouslySetInnerHtml
  style/useNodejsImportProtocol
  a11y/noDistractingElements
  suspicious/noArrayIndexKey
  complexity/noWith
  suspicious/noDuplicateClassMembers
  complexity/noExtraBooleanCast
  performance/noAccumulatingSpread
  a11y/useValidAriaProps
  a11y/noRedundantAlt
  correctness/noChildrenProp
  correctness/noUnknownFunction
  correctness/noInvalidPositionAtImportRule
  suspicious/noConfusingLabels
  suspicious/noPrototypeBuiltins
  suspicious/noConfusingVoidType
  suspicious/noFocusedTests
  a11y/useButtonType
  a11y/useSemanticElements
  a11y/noAriaUnsupportedElements
  correctness/noInvalidGridAreas
  correctness/noFlatMapIdentity
  suspicious/noSuspiciousSemicolonInJsx
  a11y/noBlankTarget
  a11y/useHeadingContent
  correctness/useValidForDirection
  correctness/noVoidTypeReturn
  correctness/noInvalidUseBeforeDeclaration
  a11y/noAriaHiddenOnFocusable
  a11y/useGenericFontNames
  correctness/noUnknownMediaFeatureName
  a11y/useAnchorContent
  complexity/noUselessRename
  style/useNumberNamespace
  complexity/noUselessConstructor
  a11y/noAccessKey
  style/useExponentiationOperator
  style/noUnusedTemplateLiteral
  complexity/noUselessSwitchCase
  correctness/noUndeclaredVariables
  style/useSingleVarDeclarator
  suspicious/noExportsInTest
  a11y/noNoninteractiveElementToInteractiveRole
  style/noCommaOperator
  suspicious/noConsoleLog
  suspicious/noDuplicateAtImportRules
  suspicious/useIsArray
  a11y/noHeaderScope
  complexity/noUselessFragments
  suspicious/noMisrefactoredShorthandAssign
  suspicious/noEmptyBlock
  complexity/noForEach
  correctness/noUnusedImports
  suspicious/noFunctionAssign

Rule name

noUndeclaredVariables

Playground link

https://biomejs.dev/playground/?lintRules=all&code=ZgB1AG4AYwB0AGkAbwBuACAATQB5AEEAcABwACgAKQAgAHsACgAgACAAZgB1AG4AYwB0AGkAbwBuACAAaABhAG4AZABsAGUAcgAoAGUAOgAgAFIAZQBhAGMAdAAuAEYAbwByAG0ARQB2AGUAbgB0ACkAIAB7AAoAIAAgACAAIAByAGUAdAB1AHIAbgAgAGUACgAgACAAfQAKAAoAIAAgAHIAZQB0AHUAcgBuACAAKAAKACAAIAAgACAAPABmAG8AcgBtACAAbwBuAFMAdQBiAG0AaQB0AD0AewAoAGUAKQAgAD0APgAgAGgAYQBuAGQAbABlAHIAKABlACkAfQA%2BAAoAIAAgACAAIAA8AC8AZgBvAHIAbQA%2BAAoAIAAgACkACgB9AAoACgBlAHgAcABvAHIAdAAgAHsAIABNAHkAQQBwAHAAIAB9AA%3D%3D

Expected result

Hello, we're migrating a few React 18/TS/EsLint projects into Biome. With React 18 you could use global React Types as React.JSX.Element, React.FormEvent, etc without an import statement.

But when we enable noUndeclaredVariables in Biome to check for possible bugs, we're getting a false positive on The React variable is undeclared.

A workaround is to explicitly add an import type React from 'react' on every file with React types. But we have hundreds of files that would need this. And it was working before migrating over.

Is there a better solution for this issue?

Thanks, and we love Biome!

Code of Conduct

  • I agree to follow Biome's Code of Conduct
@Conaclos
Copy link
Member

Hi! You can explicitly set a global using the javascript.globals configuration.

@elawad
Copy link
Author

elawad commented Sep 19, 2024

We did try globals, though that introduces other issues.

// biome.json
"javascript": {
  "globals": ["React"]
}

For instance, in a non-TS .jsx file you can add this without an import.

// App.jsx
function App() {
  const [count, setCount] = React.useState(0);
}

Biome will allow it due to globals, but the app will error during runtime.

One other option was just to disable noUndeclaredVariables in Biome overrides for *.tsx files.

@Conaclos
Copy link
Member

Conaclos commented Sep 19, 2024

Have you suggestions to solve the issue? We could add metadata to globals (if it is a type, a writable flobal, ...).

Also, we have a jsxRuntime configuration. i am unsure if we could leverage it in some way.

@arendjr
Copy link
Contributor

arendjr commented Sep 19, 2024

I think the jsxRuntime should apply here indeed, but not sure if it does 😅

I’m also used to just disabling this rule, since it’s kinda redundant if you also use tsc.

@elawad
Copy link
Author

elawad commented Sep 19, 2024

I don't have any suggestions unfortunately. But disabling it seems to be ok, since TypeScript will handle undeclared issues on its own in .tsx files.

We do run biome check and tsc --noEmit in git hooks already. So that will find both types of issues.

@Conaclos
Copy link
Member

This one of the reason noUndeclaredVariabels is still not recommended. It reports too much false positive. We hope to improve it once we have proper type info.

@elawad
Copy link
Author

elawad commented Sep 19, 2024

Thanks @Conaclos, it does help in .js/.jsx files. As we're used to it coming from ESLint.
But using overrides was the perfect solution here.

@Conaclos
Copy link
Member

Conaclos commented Sep 19, 2024

It seems that there are more false positives with types than variables. We could add an option to ignore undeclared types. We could allow the option by default and allow the rule by default in Biome 2.0.

What do you think?

@elawad
Copy link
Author

elawad commented Sep 19, 2024

That's a good idea. Since types are already checked by TypeScript tsc.

@marmikdesai
Copy link

I am facing same issue. Is there a plan to fix this?

@ematipico
Copy link
Member

@marmikdesai please read the whole conversation, because it answers your question

@kurtextrem
Copy link

We could add an option to ignore undeclared types.

This would be awesome!

@Conaclos
Copy link
Member

Conaclos commented Nov 4, 2024

Actually, I think we should always ignore types in noUndeclaredVariables because the equivalent ESLint rule no-undef only check JS and TypeScript ESLint doesn't provide any extension for this rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants