question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript definition for SFC returning fragment/string

See original GitHub issue

Do you want to request a feature or report a bug?

Most likely a bug

What is the current behavior?

https://codesandbox.io/s/r555ro878p

If you open that sandbox and wait for a compiler to catch up, you see that TextReturningComponent has an error. It is expecting ReactElement<any> | null to be returned.

const TextReturningComponent: React.SFC = () => 'sometext'

What is the expected behavior?

Based on the blog post it should be possible to have a component like that. Is that correct?

Looking at the type definition for render method it has return type ReactNode which is correct. However, the SFC declaration looks like this…

interface StatelessComponent<P = {}> {
  (props: P & { children?: ReactNode }, context?: any): ReactElement<any> | null;
}

I assume this is just an oversight and there should be ReactNode type as well? In case that’s true, I am able to create PR for that. Correct me if I am missing something here, please 😃

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

"react": "^16.2.0",
"@types/react": "16.0.36",

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
gaearoncommented, Feb 5, 2018

Also, note that there are currently no differences between what an SFC can return, and what a class component can return in render. They have full feature parity.

2reactions
danielkczcommented, Feb 5, 2018

I’ve just noticed that returning fragment works just fine (could be a current workaround)

const TextReturningComponent: React.SFC = () => (
  <>
    'sometext'
  </>
)

Looking closely at this, the fragment return type is JSX.Element which is declared as interface Element extends React.ReactElement<any> { } and that’s correct. However, a string is just a string.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript definition for recursive function which returns the ...
Let's say we have a function which should process any nested object and return a new object with the same keys recursively.
Read more >
Documentation - Declaration Reference - TypeScript
The purpose of this guide is to teach you how to write a high-quality definition file. This guide is structured by showing documentation...
Read more >
Notes on TypeScript: ReturnType - DEV Community ‍ ‍
ReturnType accepts a function and returns the return type that the function would return when being invoked. number in our getInt example.
Read more >
Writing njs code using TypeScript definition files - Nginx.org
TypeScript supports definition files that contain type information of existing JavaScript libraries. This enables other programs to use the values defined in ...
Read more >
react-to-typescript-definitions - npm
Create typescript definitions files (d.ts) from react components. ... Returns the typescript definition for the given file.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found