0

Trying to follow https://medium.com/@thehappybug/using-react-context-in-a-typescript-app-c4ef7504c858 but the below confuses me. Is the AppContextInterface saying that's what the createContext expects as an argument or what it will return?

Example

const {Provider, Consumer} = React.createContext<AppContextInterface>();

stackjlei
  • 9,485
  • 18
  • 65
  • 113
  • I think this is what you are looking for: https://stackoverflow.com/questions/38831342/what-does-enclosing-a-class-in-angle-brackets-mean-in-typescript – dejoma Jun 11 '20 at 23:02
  • Neither, really. It is an explicitly provided [generic](https://www.typescriptlang.org/docs/handbook/generics.html) parameter, but how that generic is used and what will be inferred from it will depend on the specifics of `createContext`'s type. – CRice Jun 11 '20 at 23:02
  • @dejoma thanks, that explains for a variable assignment but I'm confused on it being used as part of a function – stackjlei Jun 11 '20 at 23:08
  • @stackjlei in the tutorial it says that the createContext func infers the type from its argument. To make this function expect a "YourOwnCreationType" they with angular brackets to that specific type. I think CRice is trying to explain the same thing – dejoma Jun 11 '20 at 23:13
  • @dejoma thanks for explaining, just wanna make sure I get it - so under the hood, React's `createContext` is expecting a generic? – stackjlei Jun 11 '20 at 23:30

1 Answers1

0

"AppContextInterface" in your example is the type of the argument which will be passed to the createContext function i.e. what type should go in ().

Its called type assertion

Jeremy
  • 1,170
  • 9
  • 26