Why are the partial operator `?` and explicit `undefined` incompatible?

I define the type:
import { type } from 'arktype';

const checkType = type({
'redirect?': 'string'
});

checkType({}); // pass
checkType({ redirect: undefined }); // ArkTypeError
import { type } from 'arktype';

const checkType = type({
'redirect?': 'string'
});

checkType({}); // pass
checkType({ redirect: undefined }); // ArkTypeError
I don’t understand why it is designed like this, as far as I know, partial operator will accept explicit undefined in TypeScript. (or I’m wrong ?)🤨 #questions #typescript
9 Replies
LukeAbby
LukeAbby6d ago
I can only guess as to why ArkDavid decided to design it this way since I am not him. It's probably because of exactOptionalPropertyTypes
TSConfig Reference - Docs on every TSConfig option
From allowJs to useDefineForClassFields the TSConfig reference includes information about all of the active compiler flags setting up a TypeScript project.
LukeAbby
LukeAbby6d ago
There are a number of functions where passing { x: undefined } will cause an error but {} will not for example
function doesStuff(options: { x?: number }): void {
const mergedOptions = {
x: 1,
...options,
}

mergedOptions.x * 5;
}
function doesStuff(options: { x?: number }): void {
const mergedOptions = {
x: 1,
...options,
}

mergedOptions.x * 5;
}
Will error when called as doesStuff({ x: undefined })
Dimava
Dimava6d ago
ArkType explicitly requires you to use exactOptionalPropertyTypes If you don't, you should get a type error saying to add it to tsconfig
Asfamilybank
AsfamilybankOP6d ago
So ArkType will read and use the configuration from tsconfig.json? Including when packing. How to set the exactOptionalPropertyTypes in global? I try it:
import { configure } from 'arktype/config'

configure({
exactOptionalPropertyTypes: false
})
import { configure } from 'arktype/config'

configure({
exactOptionalPropertyTypes: false
})
or
// tsconfig.json
{
"compilerOptions": {
"exactOptionalPropertyTypes": false
}
}
// tsconfig.json
{
"compilerOptions": {
"exactOptionalPropertyTypes": false
}
}
None of them take effect
Dimava
Dimava6d ago
No, it's a requirement, not a flag Nvm is was strictNullChecks
Dimava
Dimava6d ago
No description
Dimava
Dimava6d ago
Installation You'll also need... - TypeScript version >=5.1. - A package.json with "type": "module" (or an environment that supports ESM imports) - A tsconfig.json with... - strict or strictNullChecks (required) - skipLibCheck (strongly recommended, see FAQ) - exactOptionalPropertyTypes (recommended)
But how to use exactOptionalPropertyTypes=false 🤔 @Asfamilybank for now just be verbose and
import { type } from 'arktype';

const checkType = type({
'redirect?': 'string | undefined'
});

checkType({}); // pass
checkType({ redirect: undefined }); // ArkTypeError
import { type } from 'arktype';

const checkType = type({
'redirect?': 'string | undefined'
});

checkType({}); // pass
checkType({ redirect: undefined }); // ArkTypeError
I guess
Dimava
Dimava6d ago
It's planned https://github.com/arktypeio/arktype/issues/1191 but not implemented
GitHub
Add an option to allow undefined for optional properties · Issue ...
Request a feature Add a global option to allow undefined as a value for optional properties. configure({ optionalAllowsUndefined: true }); 🤷 Motivation const arkUser = type({ phoneNumber: 'stri...
Asfamilybank
AsfamilybankOP6d ago
I don't want to change the existing ts configuration because that would require me to refactor the entire project's code. I think this plan is pretty good.
Want results from more Discord servers?
Add your server