Skip to content

Conversation

@Arnavion
Copy link
Contributor

  1. | null and | undefined where appropriate.
    • As expressed in the original PR, indexers do not have | undefined. I have added it to the ES6 collection accessors like Map.get however. I think that's better.
    • I haven't added | null or | undefined to functions that stringify their parameter and thus accept null and undefined, eg Symbol(null) is the same as Symbol("null"). It doesn't seem right to allow null just for that.

1. ES6's String.normalize's form parameter was typed as string - changed to union of string literals.

1. Map and Set methods that are specced to return this were not annotated as : this, changed accordingly.

I can split the changes into separate PRs if you want. I just fixed them as I came across them while doing the first one.

The other changes became too big and will be in another PR.

jake runtests passes.

src/lib/es6.d.ts Outdated
* @param source The source object from which to copy properties.
*/
assign<T, U>(target: T, source: U): T & U;
assign<T, U>(target: T, source: U | null | undefined): T & U;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This (and similar for the overloads below) is needed because null and undefined are allowed and ignored by Object.assign, but the compiler should not infer U as null if null is passed in, as the result T & null is meaningless.

Should I add a comment to that effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, on closer look it seems U is still specialized as null instead of {}. But also the resulting type T & null doesn't seem to have any issues either.

Should I remove the | null | undefined from this function?

Copy link

Choose a reason for hiding this comment

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

T & null and T & undefined should not be a problem, because null and undefined add no new member to the type? In fact, I think the type system could (should?) simplify T & null & undefined to just T.

If it were T | null that would be a real problem.

Copy link

Choose a reason for hiding this comment

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

BTW I don't think Object.assign is "meant" to explicitely accept undefined. It reflects the fact the the parameter is optional and that should probably be modelled with assign<T>(target: T): T, so that you can call assign(x); or maybe assign<T, U>(target: T, source?: U).

Note that removing null and undefined from being explicit in the signature doesn't change the fact that assign(x, null) is still valid.

Of course the right thing to do here would be variadics (2.1) since the real signature is assign<T, ...U>(target: T, ...source: ...U): T & ...U.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

T & null and T & undefined should not be a problem, because null and undefined add no new member to the type?

Yes, it appears so, but...

In fact, I think the type system could (should?) simplify T & null & undefined to just T.

It doesn't, hence my initial concern that this would be problematic. But it seems to be fine regardless, in that the instances of T & null appear to be usable as T everywhere. I'm waiting for the TS team to confirm / deny if this is expected behavior or a bug.

BTW I don't think Object.assign is "meant" to explicitely accept undefined.

Yes, this is what I thought as well. I'm not familiar with all of assign's uses, so if it's not expected that users will want to use it with null / undefined sources then it would be nice to have a way to specify that U can't be null. Perhaps U extends {} ? I'm not in front of tsc to try at the moment.

Copy link

Choose a reason for hiding this comment

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

Since this is brand new stuff, maybe @ahejlsberg should add a reduction rule from T | null | undefined to T... that may simplify a few other places where this might come up?

Copy link
Contributor

Choose a reason for hiding this comment

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

do not think this is the correct change here. the current signature declaration is better. i think what you want is a type operator like the expression operator ! (which does not exist) on the return type to indicate that null and undefined are ignored. e.g. assign<T, U>(target: T, source: U): T! & U!;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted.

@Arnavion Arnavion mentioned this pull request Mar 23, 2016
* predicate. If it is not provided, undefined is used instead.
*/
findIndex(predicate: (value: number) => boolean, thisArg?: any): number;
findIndex(predicate: (value: number) => boolean, thisArg?: any): number | undefined;
Copy link

Choose a reason for hiding this comment

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

predicate is incorrect, it should be the same as for find().
See https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/TypedArray/findIndex

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Arnavion feel free to send a separate PR for the predicate changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mhegazy And should I move the other commits after the first one into a separate PR as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

On a second thought, the commit messages look clear enough, so feel free to include the findIndex chances in this PR if you like or have it in a separate one. up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed them from this PR. Will make another one.

@Arnavion Arnavion changed the title Add nullability annotations to core.d.ts and es6.d.ts (and other changes) Add nullability annotations to core.d.ts and es6.d.ts Mar 23, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Mar 23, 2016

thanks!

@mhegazy mhegazy merged commit 560ab23 into microsoft:master Mar 23, 2016
@Arnavion Arnavion deleted the lib-d-ts-fixes branch March 23, 2016 23:48
@Arnavion
Copy link
Contributor Author

The other changes are in #7664


// Non-standard extensions
compile(): RegExp;
compile(): this;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think there is a guarantee that compile returns your subclass if you subclass RegExp, so it should return plain RegExp instead.

Copy link

Choose a reason for hiding this comment

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

According to ES specs it should return the this value of the compile() call.
So using this type here seems like the correct thing to do.

BTW: this method is deprecated so hopefully it won't matter much.

Copy link
Contributor

Choose a reason for hiding this comment

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

Concur 👍

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants