Skip to content

Type narrowing seems to work until calling a function #38257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
eamodio opened this issue Apr 30, 2020 · 2 comments
Closed

Type narrowing seems to work until calling a function #38257

eamodio opened this issue Apr 30, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@eamodio
Copy link

eamodio commented Apr 30, 2020

TypeScript Version: 3.9.1-rc

Search Terms:

Expected behavior:
Type should be narrowed to successfully call the action method

Actual behavior:
Error

Related Issues:

Code

export type StepState<T = {}> = T & { counter: number; confirm?: boolean; startingStep?: number };

interface Repository {
	path: string;
}

interface State {
	repos: string | string[] | Repository | Repository[];
	createBranch?: string;
}

function steps(state: StepState<State>) {
	if (state.repos != null && !Array.isArray(state.repos)) {
		// Doesn't work without the cast, or the checks below
		state.repos = [state.repos] as string[] | Repository[];

		// if (typeof state.repos === 'string') {
		// 	state.repos = [state.repos];
		// } else {
		// 	state.repos = [state.repos];
		// }
	}

	// With the cast above, this will be:
	// (property) State.repos: string[] | Repository[]
	state.repos; 
	
	// But it can't be passed here, because it seems the type gets unnarrowed
	// Argument of type 'StepState<State>' is not assignable to parameter of type '{ counter: number; confirm?: boolean | undefined; startingStep?: number | undefined; } & { repos?: string[] | Repository[] | undefined; }'.
	//   Type 'StepState<State>' is not assignable to type '{ repos?: string[] | Repository[] | undefined; }'.
	//     Types of property 'repos' are incompatible.
	//       Type 'string | Repository | string[] | Repository[]' is not assignable to type 'string[] | Repository[] | undefined'.
	//         Type 'string' is not assignable to type 'string[] | Repository[] | undefined'.(2345)
	action(state);
}

function action<State extends StepState & { repos?: string[] | Repository[] }>(state: State) {

}
Output
function steps(state) {
    if (state.repos != null && !Array.isArray(state.repos)) {
        // Doesn't work without the cast, or the checks below
        state.repos = [state.repos];
        // if (typeof state.repos === 'string') {
        // 	state.repos = [state.repos];
        // } else {
        // 	state.repos = [state.repos];
        // }
    }
    // With the cast above, this will be:
    // (property) State.repos: string[] | Repository[]
    state.repos;
    // But it can't be passed here, because it seems the type gets unnarrowed
    // Argument of type 'StepState<State>' is not assignable to parameter of type '{ counter: number; confirm?: boolean | undefined; startingStep?: number | undefined; } & { repos?: string[] | Repository[] | undefined; }'.
    //   Type 'StepState<State>' is not assignable to type '{ repos?: string[] | Repository[] | undefined; }'.
    //     Types of property 'repos' are incompatible.
    //       Type 'string | Repository | string[] | Repository[]' is not assignable to type 'string[] | Repository[] | undefined'.
    //         Type 'string' is not assignable to type 'string[] | Repository[] | undefined'.(2345)
    action(state);
}
function action(state) {
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

@nmain
Copy link

nmain commented Apr 30, 2020

Duplicate of #31755
My search terms were "narrowing parent"

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Apr 30, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants