Answer the question
In order to leave comments, you need to log in
How to assign an interface with required fields for a context without a default value?
There is a context in a separate context.ts file:
import React from 'react';
import SomeClass from '.....';
interface IContext {
someClass: SomeClass;
}
// я должен назначить контексту интерфейс, чтобы видеть в других местах приложения его конкретные свойства
const Context= React.createContext<IContext>({});
export default Context;
const someValue = 'someValue';
<Context.Provider value={{ someClass: new SomeClass(someValue ) }}>
{children}
</Context.Provider>
const Context= React.createContext<Partial<IContext >>({});
Answer the question
In order to leave comments, you need to log in
In my opinion, this is the best option:
const Context= React.createContext(null as unknown as IContext);
...
const { someClass } = useContext(Context);
const Context= React.createContext<IContext | null>(null);
interface MyLittleInterface {
age: number;
name: string;
}
const someVar: MyLittleInterface = {}; // ой-ой!
const someOtherVar: MyLittleInterface = {} as MyLittleInterface; // Ура!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question