-
Notifications
You must be signed in to change notification settings - Fork 26.1k
fix(compiler-cli): ensure literal types are retained when strictNullInputTypes
is disabled
#38305
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
Conversation
8f06afd
to
de550e1
Compare
525c749
to
27a83ad
Compare
…InputTypes` is disabled Consider the `NgModel` directive which has the `ngModelOptions` input: ```ts class NgModel { @input() ngModelOptions: { updateOn: 'blur'|'change'|'submit' }; } ``` In a template this may be set using an object literal as follows: ```html <input ngModel [ngModelOptions]="{updateOn: 'blur'}"> ``` This assignment should be accepted, as the object's type aligns with the `ngModelOptions` input in `NgModel`. However, if the `strictNullInputTypes` option is disabled this assignment would inadvertently produce an error: ``` Type '{ updateOn: string; }' is not assignable to type '{ updateOn: "blur"|"change"|"submit"; }'. Types of property 'updateOn' are incompatible. Type 'string' is not assignable to type '"blur"|"change"|"submit"' ``` This is due to the `'blur'` value being inferred to be of type `string` instead of retaining its literal type. The non-null assertion operator that is automatically inserted for input binding assignments when `strictNullInputTypes` is disabled inhibits TypeScript from inferring the string value as its literal type. This commit fixes the issue by omitting the insertion of the non-null operator for object literals and array literals.
27a83ad
to
ace6a08
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice commit. Both because it fixes the error, but also because it removes duplication and makes the code easier to understand.
This PR was merged into the repository by commit 2a27447. |
…InputTypes` is disabled (#38305) Consider the `NgModel` directive which has the `ngModelOptions` input: ```ts class NgModel { @input() ngModelOptions: { updateOn: 'blur'|'change'|'submit' }; } ``` In a template this may be set using an object literal as follows: ```html <input ngModel [ngModelOptions]="{updateOn: 'blur'}"> ``` This assignment should be accepted, as the object's type aligns with the `ngModelOptions` input in `NgModel`. However, if the `strictNullInputTypes` option is disabled this assignment would inadvertently produce an error: ``` Type '{ updateOn: string; }' is not assignable to type '{ updateOn: "blur"|"change"|"submit"; }'. Types of property 'updateOn' are incompatible. Type 'string' is not assignable to type '"blur"|"change"|"submit"' ``` This is due to the `'blur'` value being inferred to be of type `string` instead of retaining its literal type. The non-null assertion operator that is automatically inserted for input binding assignments when `strictNullInputTypes` is disabled inhibits TypeScript from inferring the string value as its literal type. This commit fixes the issue by omitting the insertion of the non-null operator for object literals and array literals. PR Close #38305
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
…InputTypes` is disabled (angular#38305) Consider the `NgModel` directive which has the `ngModelOptions` input: ```ts class NgModel { @input() ngModelOptions: { updateOn: 'blur'|'change'|'submit' }; } ``` In a template this may be set using an object literal as follows: ```html <input ngModel [ngModelOptions]="{updateOn: 'blur'}"> ``` This assignment should be accepted, as the object's type aligns with the `ngModelOptions` input in `NgModel`. However, if the `strictNullInputTypes` option is disabled this assignment would inadvertently produce an error: ``` Type '{ updateOn: string; }' is not assignable to type '{ updateOn: "blur"|"change"|"submit"; }'. Types of property 'updateOn' are incompatible. Type 'string' is not assignable to type '"blur"|"change"|"submit"' ``` This is due to the `'blur'` value being inferred to be of type `string` instead of retaining its literal type. The non-null assertion operator that is automatically inserted for input binding assignments when `strictNullInputTypes` is disabled inhibits TypeScript from inferring the string value as its literal type. This commit fixes the issue by omitting the insertion of the non-null operator for object literals and array literals. PR Close angular#38305
Consider the
NgModel
directive which has thengModelOptions
input:In a template this may be set using an object literal as follows:
This assignment should be accepted, as the object's type aligns with the
ngModelOptions
input inNgModel
. However, if thestrictNullInputTypes
option is disabled this assignment would inadvertently produce an error:
This is due to the
'blur'
value being inferred to be of typestring
instead of retaining its literal type. The non-null assertion operator
that is automatically inserted for input binding assignments when
strictNullInputTypes
is disabled inhibits TypeScript from inferringthe string value as its literal type.
This commit fixes the issue by omitting the insertion of the non-null
operator for object literals and array literals.