I have this code:
'use strict';
type Bar = 'a' | 'b';
type FooIntermidiate = {
[p in Bar]: string;
};
type Foo = Required<FooIntermidiate>;
console.log(getFoo());
function getFoo(): Foo {
const rv = Object.create(null);
//const rv = { };
rv.b = 'str';
return rv;
}
I expect the tsc
to throw errors that at least the a
property is missing on the returned value of type Foo. But this doesn't happen. If I use the line: const rv = { };
then it throws, but about both of the props: a
and b
, which is better.
But why doesn't it throw with the Object.create(null)
?
It doesn't matter if I use the Required<>
type or not.
Let's look at Object.create()
official definition:
/**
* Creates an object that has the specified prototype or that has null prototype.
* @param o Object to use as a prototype. May be null.
*/
create(o: object | null): any;
/**
* Creates an object that has the specified prototype, and that optionally contains specified properties.
* @param o Object to use as a prototype. May be null
* @param properties JavaScript object that contains one or more property descriptors.
*/
create(o: object | null, properties: PropertyDescriptorMap & ThisType<any>): any;
As we can see, it has two overloadings, both accepting object | null
. That's why you don't get the error when passing null
. Both of the overloadings return any
, so setting the property also doesn't fire any error.