Skip to content

TS 5.3で追加されたswitch(true)での型の絞り込みについて書きませんか #1131

Description

@yo-goto

TS 5.3 で追加された switch (true) での narrowing (型の絞り込み)機能について書きませんか?
https://devblogs.microsoft.com/typescript/announcing-typescript-5-3/

function f(x: unknown) {
    switch (true) {
        case typeof x === "string":
            // 'x' is a 'string' 
        case Array.isArray(x):
            // 'x' is a 'string | any[]' 
        default:
            // 'x' is 'unknown' 
    }
}

フォールスルーすることで、型が追加されていくところが特徴的な機能。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions