-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Removing optional modifier also removes undefined
from value type
#31025
Comments
I believe |
|
Unfortunately missing and undefined aren't consistently two different things in TypeScript; see #13195. Relevant documentation on the behavior of
But in {[K in keyof T]-?: Foo<T[K]>} should Assuming we can't destabilize the current behavior, someone who wants to strip the optional modifier off property keys but hold on to type NullablyRequired<T> = { [P in (keyof T & keyof any)]: T[P] }
type Test = NullablyRequired<{a?: string, b: number}>
// type Test = {a: string | undefined, b: number} |
@weswigham I think it should at least work when you explicitly declare
|
Wow had no idea there's |
Yeah I ran into this as well, i wanted to define this type but it seem like, as @jcalz suggested, the // this doesn't work
export type RequireOptionalKeysToBeSpecified<O> = {
[K in keyof O]-?: O[K] extends undefined ? O[K] | undefined : O[K]
} edit:
edit2: nevermind, you have answered that on SO already 😹 https://stackoverflow.com/a/59791889/2544629 |
The following works and is the most obvious: export type NonPartial<T> = { [K in keyof Required<T>]: T[K] }; |
Thanks for the workarounds! But could somebody please explain how this works? type NullablyRequired<T> = { [P in (keyof T & keyof any)]: T[P] } What's the significance of adding |
@Torvin I found this answer on SO helpful: https://stackoverflow.com/a/59791889/2544629 Basically |
Unfortunately this doesn't seem to work for interfaces that include dynamic fields, e.g.
It seems to give up on the non-dynamic fields defined in the interface and just infer
However, the other workaround does seem to handle this scenario
|
This is never changing, right? Can we "won't fix" this and just tell people to work around it? |
Even if changing some of the mentioned behaviors might be hard to change now (although, personally I think it's better to fix them), I feel like especially the OP's case is quite bizarre and unintuitive for users. Syntactically |
gordonmleigh you absolute king 👑 Probably hard to change the behavior of |
Now the format of the output file is combined into a full object, which is then used by the main file, rather than importing all of the arguments as individual flags. Added a flag to specify the SNBT indentation spacing! It was only set to 2 before, now you can have fully minified SNBT, or use any indentation you'd like. Like the programming NBTify API, it accepts a number or a string. Removed the `--pipe` flag, in favor of simply specifying whether you want to use an NBT or SNBT output to stdout. If neither `--nbt` or `--snbt` are passed as flags, it will simply log out the structure of the file, with nice pretty-printing, colors, and such, as it currently has been doing thus far. The pretty-print logging now fully logs out the NBT file's tree, other than for TypedArray-based tags. For everything else though, you see all of it's content, rather than being abbreviated (the normal behavior for `console.log()` in Node.js). Learned about the difference between `isNan()` and `Number.isNaN()`. The first one coerces the input value, checks if it's `NaN`, while the other simply checks what it is currently (doesn't convert to `number` first). #25 microsoft/TypeScript#31025 https://stackoverflow.com/questions/56143158/how-to-use-util-promisify-and-bind-functions-in-nodejs (I think I ran into this issue previously, but I didn't know that function binding would work nicely here!) https://stackoverflow.com/questions/43362222/nodejs-short-alias-for-process-stdout-write https://askubuntu.com/questions/510890/how-do-i-redirect-command-output-to-vim-in-bash (Looked into this again, was curious in whether it's viable to edit SNBT with Vim again) Oh yeah, that reminds me, I also made it so SNBT is added to stdout with a trailing `\n`, so it nicer looking in the terminal, as well as in the editor, as most editors utilize trailing new lines. I think those are finally catching on with me! They really bugged me for some reason, since the start of my programming lol. Now I'm getting Linux'ed haha. nodejs/node#20366 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN https://www.google.com/search?q=comments+in+powershell
Note that this doesn't seem to actually work unless you are using typescript 5.5 or later. Prior to that, it just copies through the optionality of all fields. |
Unfortunately, there's an odd behavior with this solution. It looks correct only until you access it. type Properties = NonPartial<{ options?: object }>; // { options: object }
type Options = Properties['options']; // object | undefined The following example would have been the expected behavior. type Properties = Required<{ options?: object }>; // { options: object }
type Options = Properties['options']; // object |
@jcalz This seems to happen because of homomorphism and not because of If we add type NullablyRequired<T> = { [P in (keyof T & keyof any)]-?: T[P] } // Note the addition of `-?`
type Test = NullablyRequired<{a?: string, b: number}>
// type Test = {a: string | undefined, b: number} The type of type NullablyRequired<T> = { [P in keyof T]-?: T[P] }
type Test = NullablyRequired<{a?: string, b: number}>
// type Test = {a: string, b: number} The only difference between the two snippets above is that the latter is homomorphic while the former is not. |
I thought it was apparent that the issue has to do with using |
TypeScript Version: 3.3.3333
Search Terms: NonPartial, remove optional modifier
Code
Expected behavior:
NonPartialish.opt
type should supportundefined
.Actual behavior:
NonPartialish.opt
type does not supportundefined
.Playground Link: https://www.typescriptlang.org/play/#src=%2F%2F%20A%20*self-contained*%20demonstration%20of%20the%20problem%20follows...%0D%0A%2F%2F%20Test%20this%20by%20running%20%60tsc%60%20on%20the%20command-line%2C%20rather%20than%20through%20another%20build%20tool%20such%20as%20Gulp%2C%20Webpack%2C%20etc.%0D%0A%0D%0Ainterface%20OptClass%20%7B%0D%0A%20%20opt%3F%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Atype%20NonPartialIsh%20%3D%20%7B%5BK%20in%20keyof%20OptClass%5D-%3F%3A%20OptClass%5BK%5D%20%7C%20undefined%7D%3B%0D%0A%0D%0Aconst%20test%20%3D%20%7Bopt%3A%20undefined%7D%3B%0D%0A%0D%0Averify%3CNonPartialIsh%3E(test)%3B%20%20%2F%2F%20should%20NOT%20be%20error%2C%20but%20shows%20error%0D%0A%0D%0Afunction%20verify%3CT%3E(a%3A%20T)%20%7B%20%7D%0D%0A
Related Issues:
The text was updated successfully, but these errors were encountered: