3
export default () => (<div>hi</div>)

I'd like to annotate it as a react stateless function without giving it a name.

A named example would be:

const it: React.SFC = () => (<div>hi</div>)
export default it;
Grandtour
  • 1,127
  • 1
  • 11
  • 28
AKnox
  • 2,455
  • 3
  • 19
  • 19

2 Answers2

2

The types of variables declared within export default statements can be asserted using as

export default (() => <div>Hi</div>) as React.SFC<{}>
Arman Charan
  • 5,669
  • 2
  • 22
  • 32
  • 1
    From the page you linked: "TypeScript's type assertion is purely you telling the compiler that you know about the types better than it does, and that it should not second guess you." It appears that `as` is a *runtime* type assertion, not a *compile-time* assertion. –  Feb 21 '19 at 16:17
  • [TypeScript](https://www.typescriptlang.org/) compiles to JavaScript. [Any types that you assert using `as` are stripped away at compile-time](https://stackoverflow.com/questions/44078205/how-to-check-the-object-type-on-runtime-in-typescript) @jasonszhao – Arman Charan Mar 04 '19 at 00:01
-1

You don't.

And stackoverflow won't accept answers that don't have at least 30 characters.

AKnox
  • 2,455
  • 3
  • 19
  • 19