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

@bem-react/di: Check type when setting values in registry #386

Open
yarastqt opened this issue Jan 14, 2019 · 2 comments · May be fixed by #462
Open

@bem-react/di: Check type when setting values in registry #386

yarastqt opened this issue Jan 14, 2019 · 2 comments · May be fixed by #462

Comments

@yarastqt
Copy link
Member

Неплохо было бы добавить проверку типов при установлении значений в реестре:

interface RegistryComponents {
  ComponentName: ComponentType<ComponentProps>
}

const registry = new Registry<RegistryComponents>({ id: 'registry-id' })

// Component должен быть типом ComponentType<ComponentProps>
registry.set('component-key', Component)

Но могут возникнуть проблемы при переопределении уже существующего реестра:

// Скорее всего ругнется, т.к. компонент EnhancedComponent будет иметь другой тип
registry.set('component-key', EnhancedComponent)
@belozer
Copy link
Member

belozer commented Aug 3, 2019

В таком кейсе, нужно и сразу наверное подумать о переопределениях (например эксперименте).

На ум приходит дешёвое решение:

const expRegistry = new Registry<Partial<RegistryComponents>>({ id: 'registry-id' })

И немного дороже

const expRegistry = new RegistryLayer<RegistryComponents>({ id: 'registry-id' })

Но возможно будет польза в каких-то подкапотных операций.

@belozer
Copy link
Member

belozer commented Aug 3, 2019

@yarastqt у меня вроде хорошие новости

interface IComponent { className: string }
interface IEnhancedComponent extends IComponent { prop: string }
interface IOtherComponent { prop: number }
interface IRegistry { Component: IComponent }

const registry: Partial<IRegistry> = {}
const Component: IComponent = { className: '' }
const EnhancedComponent: IEnhancedComponent = { className: '', prop: '' }
const OtherComponent: IOtherComponent = { prop: 35 }

registry.Component = Component; // OK
registry.Component = EnhancedComponent; // OK
registry.Component = OtherComponent; // TS ERROR

TypeScript Playground

@belozer belozer linked a pull request Aug 9, 2019 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants