Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

typescript - Type intersections using any

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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: unknown; // 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...