Type intersections using any

与世无争的帅哥 提交于 2019-12-29 08:15:10

问题


From https://github.com/Microsoft/TypeScript/pull/3622:

Supertype collapsing: A & B is equivalent to A if B is a supertype of A.

However:

type a = string & any; // Resolves to any, not string!?

This intersection resolves to any. Isn't 'any' a supertype of string? So shouldn't this intersection be just string, due to supertype collapsing? What am I missing?

The use case here is something like:

type PropertyMap = {
    prop1: {
        name: "somename";
        required: any;
    };
    prop2: {
        name: "someothername";
        required: never;
    }
}

type RequiredOnly = {
    [P in keyof PropertyMap]: PropertyMap[P] & PropertyMap[P]["required"]
}

// RequiredOnly["prop2"] correctly inferred to be never, but we've
// lost the type info on prop1, since it is now an any (but should
// have been narrowed to it's original type).

Any help appreciated.


回答1:


In TypeScript, any is an escape hatch from the type system. Or maybe a black hole that eats up every other type it touches. It is treated both as a top type (any value can be assigned to a variable of type any) and a bottom type (a value of type any can be assigned to a variable of any type). You might even say it is both a supertype of string and a subtype of string. That's generally unsound; all types become assignable to all other types if you use any, but it's a useful way to opt out of the type system and make assignments that the compiler would otherwise prevent.

If you want a real top type which isn't a black hole, use unknown. You already know that never is the real bottom type. For more interesting reading on this, see Microsoft/TypeScript#9999.

For your code, try:

type PropertyMap = {
    prop1: {
        name: "somename";
        required: {}; // top type
    };
    prop2: {
        name: "someothername";
        required: never; // bottom type
    }
}

type RequiredOnly = {
    [P in keyof PropertyMap]: PropertyMap[P] & PropertyMap[P]["required"]
}

Now RequiredOnly["prop1"] should act like what you want.

Hope that helps; good luck!


Any help appreciated.

I see what you did there.



来源:https://stackoverflow.com/questions/46673558/type-intersections-using-any

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!