Closed samreid closed 2 years ago
Perhaps we will implement using https://nishanths.svbtle.com/a-stricter-omit-type
Discussed 5/26/22 - We would like to use a third party/our own Omit
that is type safe.
@samreid said that making Omit
type safe may be a small change that we could add to phet-core. That would be better than pulling in a third party dependency.
1) We will add an eslint rule (probably bad-text) to find usages. 2) We will create our own Omit and then use.
The likely change to omit will be (from https://github.com/phetsims/phet-core/issues/119#issuecomment-1138924847)
type OmitStrict<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
from
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
@zepumph and @AgustinVallejo volunteered to work on this.
https://github.com/microsoft/TypeScript/issues/30825#issuecomment-493605690
Shows this definition.
export type Omit<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;
https://github.com/phetsims/phet-core/issues/119#issuecomment-1138927993 is identical to https://github.com/phetsims/phet-core/issues/119#issuecomment-1138924919 but with different type parameter names.
@AgustinVallejo and I were able to make good progress on this. We used the implementation typefest. The initial find replace was quite easy:
find: \bOmit<
, replace: OmitStrict<
But then we spent a long time figuring out a way to get the imports to be brought in automatically. Here is what we did.
file[PROJECT_NAME]:scenery/nodes/Node.ts||. . . . .
.\n\n(^import.*'((\.\.\/)+).*$)
(the first import in any of those files, capturing the number of "../" for later use\n\n$1\nimport OmitStrict from '$2phet-core/js/types/OmitStrict.js';
($1 is the original import, $2 is the greedily grabbed number of ../
in the original import).from './X'
. Fixed manuallyNodeLayoutConstraint
)Omit
to the ban-types lint rule, suggesting to use OmitStrict
instead, there were no errors when we ran this new rule. Assigning to @jonathanolson to fix that one TODO (search for https://github.com/phetsims/phet-core/issues/119). Then back to @samreid to see if there is anything else, and/or to unblock the DeepOmitStrict side issue this is blocking.
@pixelzoom and @AgustinVallejo found usages in documentation that we didn't get. Yesterday we were trying to limit false positives by only looking at *.ts
files, but forgot that left out documentation files. Thanks for fixing that!
A comment on the choice of name OmitStrict ...
OmitStrict sounds like a verb + noun construction, like we're omitting something named "strict". What we actually have is adjective + noun. A name that's used in https://github.com/microsoft/TypeScript/issues/30825#issuecomment-483954049 is StrictOmit
, which makes more sense to me -- a strict (adjective) version of the Omit type (noun).
I don't feel strongly that it should be changed, but I think OmitStrict is awkward -- in usage and in conversation.
Sounds good to me! I'll go for StrictOmit
It is now StrictOmit, a much better name! Over to @jonathanolson to finish up as said in https://github.com/phetsims/phet-core/issues/119#issuecomment-1142798208.
Fixed that one case. @zepumph can you verify?
Looks great thanks. Over to @samreid to close, and note in the DeepStrictOmit issue as needed.
Looks great, thanks! Closing.
From https://github.com/phetsims/scenery-phet/issues/730#issuecomment-1125629164, we discovered that Omit does not check the type of its keys:
This problem, alternatives, lint rules to avoid using the built-in
Omit
type are discussed in https://github.com/microsoft/TypeScript/issues/30825Let's discuss this at an upcoming developer meeting. But we can also take the temperature with emojis like so: