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

Got TS2322 error when instantiate FormControl<string> with typescript 'strict' flag #46494

Closed
ISTAnsver opened this issue Jun 24, 2022 · 2 comments

Comments

@ISTAnsver
Copy link

Which @angular/* package(s) are the source of the bug?

forms

Is this a regression?

No

Description

Hello everyone!

I'm not sure but seems it's strange behaviour when I try to instantiate FormControl<string> object. I have enabled strict mode for TypeScript and then I've got the next error message from compiler:

error TS2322: Type 'FormControl<string | null>' is not assignable to type 'FormControl<string>'.
  Type 'string | null' is not assignable to type 'string'.
    Type 'null' is not assignable to type 'string'.

I don't wanna change generic type of declared variable to string | null. I absolutely know that null value isn't possible and it must be forbiden to assign. Field is initialized with empty string in the constructor. If null is possible then once it will be certainly happened .
I think that this behaviour should be changed.

It's relative to #13721 feature.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-n7wfaq?file=src/app/hello.component.ts

Please provide the exception or error you saw

error TS2322: Type 'FormControl<string | null>' is not assignable to type 'FormControl<string>'.
  Type 'string | null' is not assignable to type 'string'.
    Type 'null' is not assignable to type 'string'.

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.0.0
Node: 16.13.2
Package Manager: npm 8.1.2
OS: win32 x64

Angular: 14.0.0
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, material, platform-browser, platform-browser-dynamic
... router

Anything else?

No response

@JoostK
Copy link
Member

JoostK commented Jun 24, 2022

This is working as intended; you have to use the nonNullable option:

    this.name = new FormControl<string>('', {
      asyncValidators: [],
      validators: [],
      nonNullable: true,
    });

Please consult the typed forms docs for more information.

@JoostK JoostK closed this as completed Jun 24, 2022
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants