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

feat(di): check types in registry #462

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

feat(di): check types in registry #462

wants to merge 1 commit into from

Conversation

belozer
Copy link
Member

@belozer belozer commented Aug 9, 2019

resolve #386

Example:

interface MyReg {
    A: React.ComponentType<{ a: string }>,
    C: React.ComponentType<{ c: string }>
}

const id = 'RegistryId'

const A: React.FC<{ a: string }> = () => null
const B: React.FC<{ b: string }> = () => null
const C: React.FC<{ c: string }> = () => null
const AB: React.FC<{ a: string, b: string }> = () => null

Case 0: Pre check (missing props)

const registry = new Registry<MyReg>({ id }, { 
  A,
  // TS Error: Property 'C' is missing
});

const expRegistry = new Registry<Partial<MyReg>>({ id }, { 
  A
}); // Ok

Case 1: Pre check

const registry = new Registry<MyReg>({ id }, {
  A, 
  B, // TS Error: 'B' does not exist in type 'MyReg' 
  C
});

Case 2: Post check

const registry = new Registry<MyReg>({ id });

registry.set('A', A); // Ok
registry.set('A', B); // TS Error
registry.set('A', AB); // Ok

Case 3: Hints
image
image

packages/di/di.tsx Outdated Show resolved Hide resolved
packages/di/di.tsx Outdated Show resolved Hide resolved
packages/di/di.tsx Outdated Show resolved Hide resolved
@belozer belozer changed the title feat(di): check types in registy feat(di): check types in registry Aug 11, 2019
packages/di/di.tsx Outdated Show resolved Hide resolved
packages/di/di.tsx Outdated Show resolved Hide resolved
packages/di/di.tsx Outdated Show resolved Hide resolved
id: string;
overridable: boolean;
private components: IRegistryComponents = {};
private components = {} as RTC;
Copy link
Member

@yarastqt yarastqt Aug 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А разве без кастинга тут не получится?

private components: RTC = {}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yarastqt неа. TS на подтипы ругается.

Type '{}' is not assignable to type 'RTC'.
  '{}' is assignable to the constraint of type 'RTC', but 'RTC' could be instantiated with a different subtype of constraint 'IRegistryComponents'.

через приведение к типу всё ок.

packages/di/di.tsx Outdated Show resolved Hide resolved
packages/di/di.tsx Outdated Show resolved Hide resolved
packages/di/di.tsx Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@bem-react/di: Check type when setting values in registry
3 participants